Thread: Static Locals VS Globals (Mem Location)

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Static Locals VS Globals (Mem Location)

    I have quite a lot of variables which change/update as i repaint... I've opted to make all of these static locals instead of Globals... This forces my code to have quite a lot of "static" keyword in my WndProc() function... I'm starting to see some misbehavior in some of these variables... Or this could be some logic in my program.. Question is, would it be a problem to have too many static variables as oppose to local variables, or global for that matter.. Would i have some memory issues, not sure how statics are handles as far as memory is concerned

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Static local variables and global variables should, in general, be placed in the same location in memory... at the very least, not on the stack.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    static variables (whether they are local to a function or not) are the same as global variables - the only difference is that they are not made visible outside of their current scope (file or function [or class in C++]), so there's no problem with having five different variables called "lastEntry" in different places - in a regular global variable that would either turn into one variable or it would give linker errors for "Multiple symbols".

    There is no strict limit of the size of static variables - obviously, there will be some limit in all systems, but if you are using Windows, it would be feasible to have more than 1GB of data (depending on the settings of your system, the limit for one executable is 2GB or 3GB, minus a tiny bit for "admin purposes").

    --
    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. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM