Array access problem / memory location conflict

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Array access problem / memory location conflict

跳至解决方案
1,225 次查看
alexanderbuerge
Contributor II

Hi there!

I am pretty new to codewarrior. I am using Codewarrior IDE 5.9.0 Build 5294. All settings are default since it is a new installation. I generated a project for MC9S12XEQ512 in single chip mode using "device initialization wizzard".

In my main.c I have declared an array "unsigned char  dataBuffer[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};"

My problem now is, that, for reasons I don't know the values within this array seem to randomly change.

To me, it looks as if some other operations do take place in the same RAM memory location as my array is located in, and therefore the values in the array get mixed up...

I am using interrupts, but they do not access this array.

 

Has anybody an idea what might cause this problem?

 

I already figured out that the problem seems to disappear when I declare the array to be "static unsigned char"...

 

Kind regards,

Alex

标签 (1)
0 项奖励
回复
1 解答
1,024 次查看
bigmac
Specialist III

Hello Alex,

You could check whether the variable corruption problem disappears when you increase the stack size from the default setting.  Should stack overflow occur, this can overwrite one or more global variables.  Using the 'static' modifier may result in a different position for this variable, relative to the bottom of the stack.  Check the project map file for the various global variable positioning.

A larger stack is likely to be necessary if you are using floating point variables and/or are using library functions such as printf(), sprintf(), etc.

Regards,

Mac

在原帖中查看解决方案

0 项奖励
回复
3 回复数
1,024 次查看
alexanderbuerge
Contributor II

Thank you a lot Pascal Irrle and bigmac.

Both of your replies were very helpful. I think that now I know much more about how the compiler works. I read the definitions of the keywords "static, extern and volatile".

In deep I am using library functions "stdlib.h" and "string.h".

I now enlarged the stack size and correctly defined my variables (at least I hope so). Now the error does not occur any more.

Kind regards and thanks!

Alex

0 项奖励
回复
1,025 次查看
bigmac
Specialist III

Hello Alex,

You could check whether the variable corruption problem disappears when you increase the stack size from the default setting.  Should stack overflow occur, this can overwrite one or more global variables.  Using the 'static' modifier may result in a different position for this variable, relative to the bottom of the stack.  Check the project map file for the various global variable positioning.

A larger stack is likely to be necessary if you are using floating point variables and/or are using library functions such as printf(), sprintf(), etc.

Regards,

Mac

0 项奖励
回复
1,024 次查看
trytohelp
NXP Employee
NXP Employee

Hello Alex,

Are you using the dataBuffer[16] in your application ?

By default, the compiler and linker optimize the code.

This is may be due to an optimization.

Most of the time we use the volatile keyword to break optmization.

To investigate the problem, is it possible to have an example showing the behavior ?

Pascal