Thread: Static Local Variable vs. Global Variable

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

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