Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the memory layout of a C/C++ program?

Tags:

c++

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?

like image 244
Sam Avatar asked Jun 20 '10 18:06

Sam


People also ask

What is the memory layout of C program?

A C program memory layout in C mainly comprises six components these are heap, stack, code segment, command-line arguments, uninitialized and initialized data segments. Each of these segments has its own read, write permissions.

How is the layout of memory?

Memory is laid out in sequential order basically from 0 on up (one byte at a time). Each position in memory has a number (called its address!). The compiler (or interpreter) associates your variable names with memory addresses.

How does memory work in C?

In C, dynamic memory is allocated from the heap using some standard library functions. The two key dynamic memory functions are malloc() and free(). The malloc() function takes a single parameter, which is the size of the requested memory area in bytes. It returns a pointer to the allocated memory.

How is memory managed in C?

In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.


1 Answers

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.

like image 175
Puppy Avatar answered Oct 14 '22 23:10

Puppy