tags:

views:

159

answers:

4

I know that there are sections like Stack, Heap, Code and Data. Stack/Heap do they use the same section of memory as they can grow independently? What is this code section? When I have a function is it a part of the stack or the code section? Also what is this initialized/uninitialized data segment?

Are there read only memory section available? When I have a const variable, what is actually happening is it that the compiler marks a memory section as read only or does it put into a read only memory section.

Where are static data kept? Where are global data kept?

Any good references/articles for the same?

I thought the memory sections and layout are OS independent and it has more to do with compiler. Doesn't Stack, Heap, Code, Data [Initialized, Uninitialized] segment occur in all the OS? When there is a static data, what is happening the compiler has understood it is static, what next, what will it do? It is the compiler which is managing the program and it should know what to do right? All compilers shouldn't they follow common standards?

+4  A: 

It's mostly OS dependant.

For windows take a look at Petzold or Richter's books. For Linux you can read the source !

Martin Beckett
Read the source ? That sounds an awful lot like RTFM :p
Matthieu M.
I think he needs help with Reading The F'ing Memory ;)
MSalters
@Matthieu M I did put a "!" ;-)
Martin Beckett
+1  A: 

AFAIK:

Stack/Heap do they use the same section of memory as they can grow independently?

They can grow indipendently.

What is this code section?

A read-only segment where code and const data are stored.

When I have a function is it a part of the stack or the code section?

The definition (code) of the function will be in the CS. The arguments of each call are passed on the stack.

Also what is this initialized/uninitialized data segment?

The data segment is where globals/static variables are stored.

Are there read only memory section available?

The code segment. I suppose some OS's might offer primitives for creating custom read-only segments.

When I have a const variable, what is actually happening is it that the compiler marks a memory section as read only or does it put into a read only memory section.

It goes into the CS.

Where are static data kept? Where are global data kept?

The data segment.

Mau
Readonly segments were very popular with early Windows versions, it lets you run multiple copies of an app without using lots of scarce memory. Even the string resources ended up in shared readonly segments.
Martin Beckett
+2  A: 

There's very little that's actually definitive about C++ memory layouts. However, most modern OS's use a somewhat similar system, and the segments are separated based on permissions.

Code has execute permission. The other segments don't. In a Windows application, you can't just put some native code on the stack and execute. Linux offers the same functionality- it's in the x86 architecture.

Data is data that's part of the result (.exe, etc) but can't be written to. This section is basically where literals go. Only read permission in this section.

Those two segments are part of the resulting file. Stack and Heap are runtime allocated, instead of mapped off the hard drive.

Stack is essentially one, large (1MB or so, many compilers offer a setting for it) heap allocation. The compiler manages it for you.

Heap memory is memory that the OS returns to you through some process. Normally, heap is a heap (the data structure) of pointers to free memory blocks and their sizes. When you request one, it's given to you. Both read and write permissions here, but no execute.

There is read-only memory(ROM). However, this is just the Data section. You can't alter it at runtime. When you make a const variable, nothing special happens to it in memory. All that happens is that the compiler will only create certain instructions on it. That's it. x86 has no knowledge or notion of const- it's all in the compiler.

DeadMG
Much of what you wrote, e.g. "stack is essentially one large heap allocation" or "x86 has no notion of `const`", is badly inaccurate. I appreciate that you're trying to explain this without talking about virtual addressing, but virtual memory really is key to how all of this works. Especially page tables, which are where the permissions of each section are defined. And of course all of this varies *significantly* on non-x86 processors.
Ben Voigt
I'm fully aware of virtual addressing. As you said, I'm trying to keep it simple.
DeadMG
A: 

(Note: The following applies to Linux)

The stack and heap of a process both exist in the "same" part of a process's memory. The stack and heap grow towards each other (initially, when the process is started, the stack occupies the entire area that can be occupied by the combination of the stack and the heap; each memory allocation (malloc/free/new/delete) can push the boundary between the stack and the heap either up or down). The BSS section, also located on the same OS-allocated process space, is in its own section and contains global variables. Read-only data resides in the rodata section and contains such things as string literals. For example, if your code has the line:

char tmpStr[] = "hello";

Then, the portion of the source code containing "hello" will reside in the rodata section.

A good, thorough book on this is Randall E. Bryant's Computer Systems.

jrupac
The stack does NOT occupy the entire address space. In fact, it's an error to talk about "the" stack, because each thread has its own stack, and there may be additional alternate stacks for signal handling, error recovery, etc.
Ben Voigt
What I ended to say was that the stack occupies the entire address space that can be occupied by the combination of the stack and the heap.
jrupac