Thread: Static Local Variable vs. Global Variable

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    290

    Static Local Variable vs. Global Variable

    I'm wondering what the difference is between declaring a variable globally, and declaring it as a static local variable (aside from the obvious scope difference).

    The reason for the question is: I was recently working on a graphical algorithm that uses an array (actually its a stack, but I'm going to call it an array or the terminology will get confusing).

    The array needs to be rather large - large enough that storing it on the stack would be silly and crash-prone. So I decided to declare it globally, and all was good.

    But then I started thinking, I only use the array inside of one function, and I really don't want anything else to access it. So I moved it into the function as a static local variable thinking that the compiler would store it in the data segment along with the globals. However, this causes the program to crash. I have since moved it back out to global scope, and once again all is good.

    So clearly there is a difference in the way static local variables are allocated vs. global variables. What's going on here?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Chuck it on the heap, that way you can pass it in as a pointer (to the array on the heap or stack) or create it in the algo.
    Code:
    void graphical_algo(char * array, size_t n)
    {
        /* ... */
    }
    or
    Code:
    void grapihcal_algo(void)
    {
        char * array = malloc(2500);
        /* error checking etc */
    
        /* free */
    }
    > So clearly there is a difference in the way static local variables are allocated vs. global variables. What's going on here?
    Depends how you've done it, perhaps post your example?
    Last edited by zacs7; 08-20-2008 at 05:30 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    I do understand that the heap is an alternative, but I'm not concerned about making it work, I want to know why a large static local array would cause a crash.

    Here is some code to illustrate how I declared the stack:

    Version 1 - Use a global array:
    Code:
    LineSegment stack[MAXDEPTH];
    
    int the_algorithm() {
       LineSegment *sp = stack;
       ...
       // code that uses the stack through 'sp'
       ...
    }
    Version 2 - Use a static local variable:
    Code:
    int the_algorithm() {
       static LineSegment stack[MAXDEPTH];
       LineSegment *sp = stack;
       ...
       // code that uses the stack through 'sp'
       ...
    }
    The LineSegment struct:
    Code:
    typedef struct {
       int x1, x2, y, dy;
    } LineSegment;
    Assume MAXDEPTH can be anywhere from 10,000 to 1,000,000.

    [edit]
    Also, I apologize since I don't think I was very clear. I know that this isn't defined by the standard and thus is implementation specific (I happen to be using the MinGW compiler for Windows), so I'm not expecting an answer that covers all platforms, just an idea of what my compiler might be doing under the covers here.
    [/edit]
    Last edited by arpsmack; 08-20-2008 at 05:55 PM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If I were to give you a simple response, I would then have to kill you. Just kidding. Well, short answer is, it depends. Long answer is, you could be doing craziness such as writing to memory that is not in the stack or even altering memory that is not accessable by your program. This is a classic example of "just because it compiles doesn't mean it should even work." For large pools of memory it is typically better to use malloc() (or whatever...) anyway. Plus, using static locals isn't necessarily the most thread safe way to go about life.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Problem solved. Turns out I'm a doofus and you guys should be laughing and pointing fingers at me.

    I adapted my function from a Seed-Fill algorithm published by a guy named Paul Heckbert in the 80's. Unfortunately, I didn't pay attention to the fact that his upper bounds were INCLUSIVE, and I needed exclusive bounds.

    I was trying to access pixels one past the end of the array in both the X and Y directions. For smaller 2D arrays this wasn't a big deal, but for a large array I was treading WAY out of bounds if I went one row too far, and that was causing the crash.

    Forgive my lameness, even though it is extreme.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Be kind to yourself. Otherwise we are all doofus sometimes. More times than any of us would outrightedly admit to.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree with master - I had to have my colleague point out to me that "addressing the last byte in this memory is not number X but X-1" yesterday. I should know that!

    And the reason it changed when you made it static instead of global is probably just the ordering of variables in the data segment. Previously, you'd overwrite something that didn't make it crash!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Doofus! Don't worry about it, I do it all the time (but I'm no means good, if that's what you think I'm trying to putforth) -- the last time I did such a thing was... today!

    Lost 3 marks of 12 in a prac because of it too... :'(

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 11:20 AM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  4. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  5. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM