Array access problem / memory location conflict

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Array access problem / memory location conflict

Jump to solution
529 Views
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

Labels (1)
0 Kudos
1 Solution
328 Views
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

View solution in original post

0 Kudos
3 Replies
328 Views
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 Kudos
329 Views
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 Kudos
328 Views
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