英文:
Problem with static variable initialization C
问题
以下是您要翻译的内容:
"I am facing a problem with my static variable which take a random value at the startup of the program and not initialized to 0
as I am expecting it to work.
I have this function in a first
file 1:
void setInput()
{
/*.... Code ...*/
sysUpdate(SysOutput.u16_state);
/*.... Code ...*/
}
which calls the sysUpdate function in another
file 2:
bool sysUpdate(uint16 state)
{
static uint32 u32_Counter = 0;
if (Condition)
{
u32_Counter += 30;
if (u32_Counter > 30000)
{
u32_Counter = (uint32)0;
/*.... Code ...*/
}
}
/*.... Code ...*/
}
The problem is that my u32_Counter
takes a random value despite of initializing it to 0, and my condition is always true the first time.
Do you have any idea on the reason of this problem?
Thank you for your help.
Update :
This problem was caused by a writing out of the boundries of another static array. This is what exactly caused the problem :
static uint8 state[xNUMBER][yNUMBER];
const uint8 state1[xNUMBER][yNUMBER] = {{0, 1, 0, 0}, {1, 0, 0, 0},{1, 1, 1, 1}};
const uint8 state2[xNUMBER][yNUMBER] = {{0, 1, 1, 1}, {1, 1, 0, 0},{1, 1, 1, 1}};
length = sizeof(state)/sizeof(state[0][0]);
for(i = 0; i < length; i++)
{
for (j = 0; j < length; j++)
{
if(condition)
{
state[i][j] = state1[i][j];
}
else
{
state[i][j] = state2[i][j];
}
}
}
英文:
I am facing a problem with my static variable which take a random value at the startup of the program and not initialized to 0
as I am expecting it to work.
I have this function in a first
file 1:
void setInput()
{
/*.... Code ...*/
sysUpdate(SysOutput.u16_state);
/*.... Code ...*/
}
which calls the sysUpdate function in another
file 2:
bool sysUpdate(uint16 state)
{
static uint32 u32_Counter = 0;
if (Condition)
{
u32_Counter += 30;
if (u32_Counter > 30000)
{
u32_Counter = (uint32)0;
/*.... Code ...*/
}
}
/*.... Code ...*/
}
The problem is that my u32_Counter
takes a random value despite of initializing it to 0, and my condition is always true the first time.
Do you have any idea on the reason of this problem?
Thank you for your help.
Update :
This problem was caused by a writing out of the boundries of another static array. This is what exactly caused the problem :
static uint8 state[xNUMBER][yNUMBER];
const uint8 state1[xNUMBER][yNUMBER] = {{0, 1, 0, 0}, {1, 0, 0, 0},{1, 1, 1, 1}};
const uint8 state2[xNUMBER][yNUMBER] = {{0, 1, 1, 1}, {1, 1, 0, 0},{1, 1, 1, 1}};
length = sizeof(state)/sizeof(state[0][0]);
for(i = 0; i < length; i++)
{
for (j = 0; j < length; j++)
{
if(condition)
{
state[i][j] = state1[i][j];
}
else
{
state[i][j] = state2[i][j];
}
}
}
答案1
得分: 3
除非您的环境严重受损,否则u32_Counter
的初始值保证为0
。
一个可能的解释是,这个全局变量在显示问题之前执行的代码覆盖了它。
您可以尝试通过查看映射文件或符号及其地址的列表(使用nm
或objdump
实用程序)来调查,以查看在u32_Counter
之前布置的变量或数组是什么。缓冲区溢出可能导致您观察到的现象。
您可以将定义移到函数之外,以便在初始代码执行过程中不同时间打印u32_Counter
的值。但如果您这样做,问题可能会消失,因为变量可能在数据段中的不同位置布置,因此不再被覆盖,但然后可能会覆盖其他变量,副作用可能会移到其他地方。甚至只是删除= 0
的初始化程序也可能产生这种效果。
如果您有具有观察功能的调试器,请在此变量上设置观察点,以跟踪它在何处以及如何被修改。
英文:
Unless your environment is seriously broken, the initial value of u32_Counter
is guaranteed to be 0
.
A likely explanation is this global variable is overwritten by code executed before the test that shows the problem.
You can try and investigate by looking at the map file or list of symbols and their addresses (using the nm
or objdump
utilities) to see what variable or array is laid out just before u32_Counter
. A buffer overrun may cause what you observe.
You could move the definition outside the function so you can print the value of u32_Counter
at different times during the execution of the initial code. Yet the problem might disappear if you do this as the variable may be laid out at a different place in the data segment, thus no longer overwritten, but some other variable may be overwritten then and the side effects may move somewhere else. Even just removing the = 0
initializer may have this effect.
If you have a debugger with watch capabilities, set a watch point on this variable to track where and how it gets modified.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论