where do global variables fall into the memory type?
Ok, I know about stack memory where arguments to functions are
pushed upon entry and popped upon exit. I also understand heap
memory (dynamic memory) where it is allocated and released explicitly
by commands in one's program: malloc/free in C; new/free in C++.
But where do global variables fit into the picture. Is the entire application
considered one big function whose globals are pushed at program startup
and popped at program exit?
And while I'm on the subject, how about static variables in C. They appear
inside a function, but their values are retained from call to call. Are these
in the heap memory since their values are retained?
pushed upon entry and popped upon exit. I also understand heap
memory (dynamic memory) where it is allocated and released explicitly
by commands in one's program: malloc/free in C; new/free in C++.
But where do global variables fit into the picture. Is the entire application
considered one big function whose globals are pushed at program startup
and popped at program exit?
And while I'm on the subject, how about static variables in C. They appear
inside a function, but their values are retained from call to call. Are these
in the heap memory since their values are retained?
statics are just like globals, except that you can only use their name within a limited scope. Globals are around for the entire life of your program. You can think of them being pushed before main and popped after if you like, though it's not particularly accurate
Quote:can think of them being pushed before main and popped after if you like, though it's not particularly accurate
This is cruel. Please. Please tell me. I don't care if my brain explodes with accuracy. Give me details
Their initial values are stored in a special segment of your application. When the OS loads your application, it decompresses that segment into a particular region of memory. That region is neither part of the heap, nor the stack. When the application exits, all its memory is returned to the OS automatically.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| C: Global Variables versus Parameters | Lizard Man | 10 | 4,995 |
Jan 13, 2010 08:22 PM Last Post: Lizard Man |
|
| Accessing an inherited class's variables | Tobs_ | 22 | 8,395 |
Feb 28, 2007 05:26 PM Last Post: mac_girl |
|
| Problems with variables in Obj-C | vnvrymdreglage | 16 | 5,942 |
Oct 2, 2006 10:19 PM Last Post: vnvrymdreglage |
|
| Should global variables be pointers or full objects? | ia3n_g | 1 | 2,067 |
Aug 4, 2006 05:53 PM Last Post: OneSadCookie |
|
| Arrays or variables containing executable functions | Jones | 4 | 3,752 |
Jun 2, 2006 08:35 AM Last Post: Zekaric |
|

