1

Possible Duplicate:
Stack,Static and Heap in C++

Hi guys,

I am currently preparing for interviews, and quite often i see questions like, where are "static" variables or where are "local"/"global" variables stored and im totally puzzled.

I'm aware of two kinds of memory: Stack and Heap. Other than this, are there any other types of memory, where different kind of variables are stored. At a few places, i even read something about "Data Segment", but i m not sure as to how these things map to each other. Can anyone provide me with any links/explanations to different types of memory and where all the different kinds of variables are stored, preferably related to C++.

Thanks, Chander

Community
  • 1
  • 1
Chander Shivdasani
  • 9,878
  • 20
  • 76
  • 107
  • 2
    Stack and heap aren't two _kinds_ of memory. They're just different organization of the same. Also, Wikipedia is good for learning this sort of stuff, though it might be better to get a C++ book to retain the knowledge (by writing a program that uses static variables and dynamic memory allocation). – aqua Feb 08 '11 at 04:15
  • 3
    See the accepted answer for this question: [Stack,Static and Heap in C++](http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c) – Kristopher Johnson Feb 08 '11 at 04:17

2 Answers2

3

To simplify just a bit, there are basically three main storage areas you need to be concerned with:

  1. Global data -- a single static memory location outside the stack or heap. These are the variables declared not local to any function. (The distinction evident by the C/C++ "static" keyword is really just about which parts of the program are aware of the name of a global variable.)

  2. Stack data -- dynamic, but pushes and pops with function calls.

  3. Heap data -- the stuff returned by new/malloc, truly dynamic and the memory persists until a delete/free, rather than when functions exit.

Larry Gritz
  • 13,331
  • 5
  • 42
  • 42
  • There is also program data such as string literals, in some OSes how these are stored can be fairly complex – Martin Beckett Feb 08 '11 at 04:32
  • Also, there's the text section of memory which stores the code - function pointers point to that area. – Sam Dufel Feb 08 '11 at 04:32
  • Great, clean answer. Sam, he mentioned that his answer is for the storage areas a person should be concerned with. I doubt most people would need to know about the text part of memory. – Nate Symer Jan 03 '15 at 05:05
0

There is a third kind of storage and that is called static. Essentially, this is storage for data items within the runtime memory image of the excutable. It cannot be allocated or released, and it's lifetime is essentially that of the program. In C++, class static variables are stored in this area as are globals.

In the old days, some processor architectures (particularly x86) used segmented addressing. In this case, static data would reside in the data segment. In order to access static data, you used an address relative to the segment base, which was in the DS or ES register. However, since at least the mid-1990s, OS architectures have gone to a flat memory model, where all segments coalign and cover the entire memory space. So data segments should not come up in any current interview question.

ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80