简体   繁体   English

静态变量未初始化

[英]Static variables are not being initialized

I'm using the Yagarto GCC compiler and I'm finding problems when using local static variables. 我正在使用Yagarto GCC编译器,并且在使用局部静态变量时发现了问题。 All of them are initialized by me to zero when declaring it. 声明时,所有这些都由我初始化为零。 But when debugging execution, I find they are not being initialized: 但是当调试执行时,我发现它们没有被初始化:

Example: 例:

void hello( void ){
    static int number_hellos = 0;

    number_hellos++;

    printf("%d\n", number_hellos);

}

When debugger reaches number_hellos++ , I find its initial value is not 0. Why? 当调试器达到number_hellos++ ,我发现其初始值不为0。为什么? Is there any compiler or linker flag I should enable? 我应该启用任何编译器或链接器标志吗?

This static initialization: 此静态初始化:

static int number_hellos = 0;

happens only once. 只发生一次。 Actually in compile time. 实际上在编译时。 Not when entering the function. 输入功能时不可以。

Thanks for your help 谢谢你的帮助

Finally I detected the problem: The startup code was not correctly initializing the static variables. 最终,我发现了问题:启动代码没有正确初始化静态变量。 That's the reason why the execution arrives to the function hello, the static variable is unitialized. 这就是为什么执行到达函数hello的原因,静态变量已统一。

A correct startup code fixes the problem. 正确的启动代码可以解决该问题。

Thanks again 再次感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM