2021年3月26日星期五

How to ensure a global variable is initialized?

Suppose there is a global variable defined in a C source file. The global variable is used by functions defined in the source file (but not functions in other source files). This source file does not contain main().

Is three a way to make sure the global variable must be initialized before any function in this source file is called?

One way is to initialize this global variable to a illegal value in this source file. Then, when it is used, check whether it is legal or not. If it is illegal, then it means that it has not been initialized.

But I am not sure if this is a best way. Could anybody let me know what are the common ways to ensure a global variable are properly initialized?

https://stackoverflow.com/questions/66827714/how-to-ensure-a-global-variable-is-initialized March 27, 2021 at 11:17AM

没有评论:

发表评论