ARM Technical Support Knowledge Articles


Applies to: C51 C Compiler


Information in this article applies to:


If I declare a local variable called 'foo' in the following way, is it supposed to preserve its value between function calls? We have noticed that for certain projects the value is preserved, but in other projects it is corrupted.

void bar(void)
  unsigned char foo;

  P1 = foo;


The variable foo is located in the default memory area for the selected memory model. In addition, the default memory area is also overlaid with variables that are mutually exclusive in use. This data overlaying means that local variables may retain their value depending on memory usage.

For example, suppose you are using the code above in the Small memory model. The variable foo will be located in data memory. Suppose also that there is another function with a local variable called baz. This function is never executing when baz is and vice versa. The local variables foo and baz may be overlaid so they use the same memory locations. The result is that neither variable retains its value between function calls.

Only the default memory area for the selected memory model is overlaid. For example, if you are using the Small memory model and you declare a local variable to be in pdata or xdata, the variable retains its value between function calls. However, this may change in future versions of the Compiler and must not be relied upon.

The following is from the ANSI C Standard (section Storage Duration of Objects):

"storage for the object is no longer guaranteed to be reserved when execution of the block ends in any way"



Article last edited on: 2005-07-19 10:28:26

Rate this article

Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential