where do global variables fall into the memory type?

Member
Posts: 321
Joined: 2004.10
Post: #1
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?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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 Wink
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #3
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 Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  C: Global Variables versus Parameters Lizard Man 10 6,648 Jan 13, 2010 08:22 PM
Last Post: Lizard Man
  Accessing an inherited class's variables Tobs_ 22 10,360 Feb 28, 2007 05:26 PM
Last Post: mac_girl
  Problems with variables in Obj-C vnvrymdreglage 16 7,184 Oct 2, 2006 10:19 PM
Last Post: vnvrymdreglage
  Should global variables be pointers or full objects? ia3n_g 1 2,544 Aug 4, 2006 05:53 PM
Last Post: OneSadCookie
  Arrays or variables containing executable functions Jones 4 4,978 Jun 2, 2006 08:35 AM
Last Post: Zekaric