Hi everyone,
I would like to know if something similar happen to you. Well the issue is that when I declare a variable as a global one in the header file of a module with extern sometimes it loses its value. And if I declare it as static I can not access to it from other module. As far As I understand, a static variable is the same during the all of the execution of the program and it has a space of memory reserved to it. Can anyone please clarify this?
Thanks in advance.
Static, when used for global variables just kills the name scope - the scope gets limited to a single C/C++ file.
Static, when used with a local variable, makes it into a "global" variable but only accessible inside that function.
So static variables are always given fixed addresses and gets an initial value on startup that they will keep until you assign something new. Same same as a standard global variable.
So you might have an issue with memory overwrites. Check the map file to figure out what data that is stored directly before this variable - if you find an array, it's likely that you do some operation way past the end of that array and so destroy other variables.
It is sometimes smart to add special markers around arrays. Similar to:
unsigned long marker_array1_pre = 0x12345678; struct my_rec_type array1[SOME_RANDOM_SIZE]; unsigned long marker_array1_post = 0x12345678;
Then you could take a look at these markers to spot if any memory writes to the array might have reached outside the array. A good size for the markers depends depends a bit on the record type of the array, and the write access pattern.
Anyway - it's important to actually check the contents of the markers and not just happily think the program is perfect just because array overruns just kills a marker and doesn't kill the following data.
Hi Per,
Thanks for your help.
I have understand the issue of static variables.
I would like to know if sometimes happens to you with keil that when you declare a variable as extern to meake it global and uses it loses its value at some point of the program.I mean when I change the value of this variable and then I check if this variable has the expected value it is gone, it does not happend all the time, but it sometimes happens so I do not trust in this extern variables. Which could be the reason of this issue?
Thanks in advance for your help.
"extern" doesn't make a variable global.
Extern just tells the compiler that a variable with that name and type exists, but to not allocate any memory for it - the memory is allocated in another source file where the "extern" keyword isn't used.
No, I don't have issues with global variables losing their content.
The main reason for having variables losing their value is to have wild pointers or array index overruns. So your own code destroys other variables.
Another reason is that you have a stack overflow, in wich case the stack grows into variables.
Another reason is that you are using dynamic RAM, and haven't properly initialized the memory controller to refresh the DRAM.
Another reason is that you use DMA and have incorrect code so the DMA transfer writes to the wrong address region.
As already noted, 'extern' does not "make a variable global".
What, exactly, do you mean by, "loses its value" ?
Post a minimum example which illustrates the issue you are seeing.
... could be that you have an interrupt (or interrupts) overwriting your variable?
Or perhaps you have omitted a necessary 'volatile' qualifier so that updates don't happen (get "lost")...?
Thanks everyone for your replies,
when say global variable I meant a variable with the extern identifier in file and declared in another.For example of that:
/*********/ comm.h /********/
int aDeviceList[MAX_DEVICE_NUMBER];// I declare this variable in a header file
/**********/ main.c /*********/
extern int aDeviceList[MAX_DEVICE_NUMBER]; // then in other file I tell to the compiler that I am using this variable that is declared in another file.
And the problem is that when I am using it sometines during the program execution the value asigned to this variable in a function it is not stored in it, thus the value is gone.
So what I really want to know if it is appropriate to use extern variables to transfer values between modules and functions. And if some similar problem have happenned to you. Or if it there is a bug in Keil with thid type of variables.
I hope I have explained my self properly.
Your variable is most likely getting corrupted because your stack is too small and crashing into the statics. Suggest you look at your call tree and local variable usage and get a better idea of your stack depth. The stack is often defined in your startup_arch.s file, and could be just 1KB.
So your bug not Keil's
View all questions in Keil forum