Title: RT-Thread C++初始化全局对象的时机 · RTduino/RTduino · Discussion #32 · GitHub
Open Graph Title: RT-Thread C++初始化全局对象的时机 · RTduino/RTduino · Discussion #32
X Title: RT-Thread C++初始化全局对象的时机 · RTduino/RTduino · Discussion #32
Description: RT-Thread C++初始化全局对象的时机
Open Graph Description: 原题目:警惕C++全局变量初始化 目前风险已解除,无需警惕,原因请参考答案 构造函数里初始化设备这种行为可能会引发一些行为不可控。因为构造函数什么时候初始化并不确定,例如,一旦传感器通过构造函数初始化的时机比IIc设备框架要早,那么,传感器肯定是不工作的。例如: SmartDrive smd = SmartDrive(SmartDrive_DefaultAddress); //<= 这个函数...
X Description: 原题目:警惕C++全局变量初始化 目前风险已解除,无需警惕,原因请参考答案 构造函数里初始化设备这种行为可能会引发一些行为不可控。因为构造函数什么时候初始化并不确定,例如,一旦传感器通过构造函数初始化的时机比IIc设备框架要早,那么,传感器肯定是不工作的。例如: SmartDrive smd = SmartDrive(SmartDrive_DefaultAddress); //<= 这...
Opengraph URL: https://github.com/RTduino/RTduino/discussions/32
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"RT-Thread C++初始化全局对象的时机","text":"原题目:警惕C++全局变量初始化
\n目前风险已解除,无需警惕,原因请参考答案
\n构造函数里初始化设备这种行为可能会引发一些行为不可控。因为构造函数什么时候初始化并不确定,例如,一旦传感器通过构造函数初始化的时机比IIc设备框架要早,那么,传感器肯定是不工作的。例如:
\nSmartDrive smd = SmartDrive(SmartDrive_DefaultAddress); //<= 这个函数内部调用了Wire.begin函数, 调用Wire.begin初始化的时候很有可能iic都没有初始化. 因此最好的方法是将其放在setup函数中。\n\nvoid setup()\n{\n Serial.begin(9600);\n}\n\nvoid loop()\n{\n //Print on serial monitor\n Serial.print(\"Motor powered: \" + smd.IsMotorPowered(SmartDrive_Motor_ID_1));\n\n delay(1000);\n}\n
\nhttps://github.com/RTduino/RTduino/blob/master/core/HardwareSerial.cpp#L20
\nhttps://github.com/RTduino/RTduino/blob/master/core/HardwareSerial.cpp#L34
\n但是上述代码中均有LOG_E来做检查,如果出现此问题,会立刻报警。
\n目前没有发生上述描述的情况,即便是全局对象的构造函数也可以正确获取到设备,希望是杞人忧天,对此保持关注即可,无需做调整
","upvoteCount":0,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"经过确认:
\nhttps://github.com/RT-Thread/rt-thread/blob/master/components/libc/cplusplus/cxx_crt_init.c#L68
\n全局对象的实例化,是在component级别进行初始化的。风险解除。
","upvoteCount":1,"url":"https://github.com/RTduino/RTduino/discussions/32#discussioncomment-3731441"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:52563664-677c-4d35-a0f6-a3cea9c33980 |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | C872:54073:909C5:C350F:698EC567 |
| html-safe-nonce | b5424b1157a6265d2b68cc273f4c02edfd64a59e574cfaed410e282cf21d1bca |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODcyOjU0MDczOjkwOUM1OkMzNTBGOjY5OEVDNTY3IiwidmlzaXRvcl9pZCI6Ijg4MzIwNDU2NDExNjI5OTkxNDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 87d78b171042f6bd96253d4c4578353379701f921c15dd0ddb135e2ed23f7aff |
| hovercard-subject-tag | discussion:4286356 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/RTduino/RTduino/32/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/142b1ded82104900085442be20a428af49700a1e32af07b7e1d37a9dc9feec3a/RTduino/RTduino/discussions/32 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/142b1ded82104900085442be20a428af49700a1e32af07b7e1d37a9dc9feec3a/RTduino/RTduino/discussions/32 |
| og:image:alt | 原题目:警惕C++全局变量初始化 目前风险已解除,无需警惕,原因请参考答案 构造函数里初始化设备这种行为可能会引发一些行为不可控。因为构造函数什么时候初始化并不确定,例如,一旦传感器通过构造函数初始化的时机比IIc设备框架要早,那么,传感器肯定是不工作的。例如: SmartDrive smd = SmartDrive(SmartDrive_DefaultAddress); //<= 这个函数... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb2828a801ee6b7be618f3ac76fbf55def35bbc30f053a9c41bf90210b8b72ba |
| turbo-cache-control | no-preview |
| go-import | github.com/RTduino/RTduino git https://github.com/RTduino/RTduino.git |
| octolytics-dimension-user_id | 104340484 |
| octolytics-dimension-user_login | RTduino |
| octolytics-dimension-repository_id | 437460543 |
| octolytics-dimension-repository_nwo | RTduino/RTduino |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 437460543 |
| octolytics-dimension-repository_network_root_nwo | RTduino/RTduino |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e6b91a7e6e46287d26887e3fb7a4161657bab8f7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width