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.
"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.
... 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")...?