Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Segments

Tags:

c++

memory

I'm currently learning about dynamic memory in C++ and I've found many troubles while trying to fully comprehend and understand the memory segments within a program's memory.

So we have 4 memory segments: Stack, Heap, Data and Code.

Firstly I want to see if I got the basic of it

- Stack: a memory segment which is being managed like a stack once you go into a scope, you have a limited memory allocated within that segment for that scope that shall contain info regarding that scope ( variables, etc )

- Heap: a dynamic memory segment that is unlimited and when you allocate a memory in that segment it will not be deleted as you exit the scope of the code or function used and it needs to be deleted by you or a gc ( if it is unreachable anymore )

- Code: a memory segment containing the code that needs to be executed by the CPU

- Data: a memory segment containing information regarding variables ( int x - x represents memory cell 0x0FA20F )

My first question is, did I get it right? I'm really not sure....

If I did not get it right my questions are:

  • what is each memory segment used for?

  • In which memory segment information regarding variables, for example, int x - x represents memory cell 0x0FA20F is located?

  • When using recursion, when you call a function from within the function and still have code left to execute in that function, that code is saved in what people call a Stack, to what does that refer?, the regular Stack segment? if so does that mean that within the stack regular code lines that need to be executed in your current scope are stored? so what is the Code memory segment used for?

I'd love it if someone could organize the mess that is in my mind right now and explain to me what does each memory segment fully does while referring to my questions.

like image 963
user3687265 Avatar asked Dec 05 '22 20:12

user3687265


2 Answers

You're close, but not quite there. You're referring to quite an old model, something akin to real-mode x86 memory. However, let's run with it and try to clear up some confusion...

First, C++ (as specified by the standard) doesn't make any reference to terms such as stack or heap when talking about the memory model. Those are specifics of the implementation. To proceed, let's assume that you're talking about a typical x86 (32- or 64-bit) PC implementation.

C++ itself defines the following storage durations:

  • static: Allocated when the program begins, and deallocated when the program ends. Variables with this storage duration correspond roughly to storage in the data segment on x86 systems.
  • automatic: Allocated at the beginning of the enclosing code block, and deallocated at the end of the enclosing block. These are local variables, and will be allocated on the stack in x86 systems.
  • dynamic: Allocated through new, deallocated through delete. The variable persists until deleted, and would be allocated on the heap in your implementation.
  • thread local: Allocated when the thread begins and deallocated when the thread ends.

There's also a bss segment, for zero-initialised variables.

Each function call has its own stack frame, which includes all of the variables with automatic storage duration, as well as the arguments to the function, space for the return value, and a stored copy of the state of the calling function. When the function returns, this state is restored so that you can continue where you left off. Part of this state is the instruction pointer, which is a pointer to the next instruction to be executed. The instruction sequence itself is always in the code segment; it doesn't get copied into the stack.

This is something of a simplification, and for most uses you don't need to know anything beyond the standard definition of storage durations; the rest is an implementation detail!

[Note: Modern usage tends to think in terms of memory access permissions. Code will be loaded into memory pages which are read-only and executable, while data (including the stack) will be in read-write pages that are ideally marked non-executable. Any further distinction is somewhat meaningless, except perhaps that of stack/heap.]

like image 126
Andrew Avatar answered Dec 20 '22 07:12

Andrew


My first question is, did I get it right? I'm really not sure....

You seem to talk about segments as they were in the time of real-mode x86 arch. Now everything is different.

what is each memory segment used for?

Now you should think in terms of access modes. 'Code' is the memory pages which you may 'read' and 'execute'; 'Data' = 'read' and 'write' etc.

In which memory segment information regarding variables, for example, int x - x represents memory cell 0x0FA20F is located?

Nowhere. Named variables exist only in source files (and intermediate object code files). Actual code has only 'number' addresses.

When using recursion, when you call a function from within the function and still have code left to execute in that function, that code is saved in what people call a Stack, to what does that refer?

'Code' is read-only and it's not to be saved anywhere, except where it resides. What is saved is local (i.e. stack) variables and return addresses. Both are saved on the stack.

like image 24
Matt Avatar answered Dec 20 '22 06:12

Matt