This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Static Variables

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.

Parents
  • 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.

Reply
  • 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.

Children