Memory layout - Stack & Heap Memory

Thread Starter

Kittu20

Joined Oct 12, 2022
463
I'm looking for help understanding the information given on this web page. https://www.google.com/amp/s/www.geeksforgeeks.org/memory-layout-of-c-program/amp/ I use gcc compiler on windows 10 system.

I only know that there are two types of memory in hardware, one is ROM and one is RAM. Program is stored in ROM memory and data is stored in RAM. In this scenario, i don't understand what is Heap and Stack memory. I think RAM memory is divided into two parts Stack and Heap memory. Compiler decides where to put variables on Heap or Stack based on storage classes. so can you tell me what is the advantage of storing variable on Stack and what is the advantage of storing variable on Heap.
 

MrChips

Joined Oct 2, 2009
30,708
I'm looking for help understanding the information given on this web page. https://www.google.com/amp/s/www.geeksforgeeks.org/memory-layout-of-c-program/amp/ I use gcc compiler on windows 10 system.

I only know that there are two types of memory in hardware, one is ROM and one is RAM. Program is stored in ROM memory and data is stored in RAM. In this scenario, i don't understand what is Heap and Stack memory. I think RAM memory is divided into two parts Stack and Heap memory. Compiler decides where to put variables on Heap or Stack based on storage classes. so can you tell me what is the advantage of storing variable on Stack and what is the advantage of storing variable on Heap.
Mainly semantics.

All read/write data is stored in RAM (excluding data that is in mass storage devices such as hard disk drive and memory sticks, etc.).
The heap is basically all of RAM. A section of RAM is allocated to runtime functions that need temporary storage while running. When the function terminates the storage is released.

So the answer is, as far as you are concerned, you can ignore any differences where the data is stored, whether in stack or heap.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
463
So the answer is, as far as you are concerned, you can ignore any differences where the data is stored, whether in stack or heap.
This is small code I have written. I want to understand in which memory compiler will store value and why it would store at particular section

C:
#include<stdio.h>
#include<stdlib.h>

int x = 1;                  // Global variable
        
static int y = 10;          // static global variable 

extern int y;               // extern global variable

void foo()
{
 
  int a = 2;                // Local variable   
  static int b = 3;         // static local variable

}

int main()
{
 
   int var = 1;            // local variable

   int *ptr = &var         // local variable
  
   int *dptr = malloc(sizeof(int));  // dptr is local variable
       *dptr = 1,   

  return 0;
}
 

MrChips

Joined Oct 2, 2009
30,708
Variables declared inside a function will be on the stack (unless it was declared as static).
Remember that the stack is dynamic and temporary. Storage locations are released when the function returns.

You can examine the program map to see where the variables are stored.
 

BobTPH

Joined Jun 5, 2013
8,809
Program is stored in ROM memory and data is stored in RAM
Not on Windows or any other general purpose computer. The memory may be protected from writes once the program is loaded, but the memory is RAM. Even on modern day embedded systems, the memory is flash, not ROM.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
463
I don't understand the concept of the three memory segments in the picture in the link I posted. Static variable should be stored in heap memory. But link say it store at initialized data segment
 

WBahn

Joined Mar 31, 2012
29,976
I don't understand the concept of the three memory segments in the picture in the link I posted. Static variable should be stored in heap memory. But link say it store at initialized data segment
It's a matter of abstraction. As programmers, it's sufficient to think of all of memory consisting of just "stack" and "heap" because that simple model is sufficient for their purposes.

But the nitty-gritty details are more involved in order to achieve the goals, leverage the capabilities, and operate withing the constraints of the hardware. There's also no one single approach (notice that the site you linked to starts off with "A typical memory layout..."

There are practical advantages to doing it the way they describe. Consider that your program code is nothing more than a bunch of 1s and 0s that has to be loaded into memory. But how will the static variables be initialized? Is there going to be code that executes at the beginning of the program in order to allocate the memory from the heap and initialize the values? You could certainly do it that way. But what if those variables are placed right at the end of the code and the values that they need to be initialized to are simply tacked onto the end of the code itself. Now when the program is loaded, the variables are allocated and initialized at the same time. From the programmer's perspective, it makes no difference.
 

MrChips

Joined Oct 2, 2009
30,708
I think you are looking into too much detail.

Memory is separated into ROM and RAM.
What the compiler/linker/program does with RAM is its own business.

If you have a global variable declared as
int x = 1;

it needs to be initialized.

So there are two separate issues.
1) It needs to be in a fixed location in RAM so that all functions can access it.

2) It needs to be initialized to a given value. This is done by initialization code embedded in the program.
 

WBahn

Joined Mar 31, 2012
29,976
I too feel the same.

One last, What concepts should be remembered for heap and stack in C language?
Remembered for what purpose?

One thing to keep in mind is that the very notions of "heap" and "stack" are not part of the C language -- neither of those words appear in the entire 700+ page language standard. They are merely common constructs used by nearly all implementations of the language. So on one end of the spectrum, you don't need to remember anything about them. Lot's of programmers go their entire career without them being much more than a couple of words and most people that casually write programs, in C or any other language, have no concept what what they are or how they are used. But if you are concerned with writing good, secure code, you need to have a deeper awareness of how implementations commonly use them. And if you are concerned with exploiting poor code, then you have to have an extremely detailed knowledge of how those concepts are implemented by the compiler that was used to produce the code you are trying to exploit.
 

MrChips

Joined Oct 2, 2009
30,708
I too feel the same.

One last, What concepts should be remembered for heap and stack in C language?
I think that you only thing you need to keep in mind is that the stack is used when a function is called.
Any local variables declared in the function will be located on the stack. When the function ends the variables are gone.
When the function is called again, the process is repeated.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
463
. And if you are concerned with exploiting poor code, then you have to have an extremely detailed knowledge of how those concepts are implemented by the compiler that was used to produce the code you are trying to exploit.
I understood about stack memory that when the function starts and local variables are defined in side function. The value of the variable will be stored on the stack and when the function exits the value of the variable will be destroyed.

I still don't fully understand about heap memory. is it static memory? like a static local variable that is initialized only once. static variable is not initialized and destroyed on each function call
 

WBahn

Joined Mar 31, 2012
29,976
I understood about stack memory that when the function starts and local variables are defined in side function. The value of the variable will be stored on the stack and when the function exits the value of the variable will be destroyed.
The value of the variables on the stack are not destroyed when the function exists. The memory locations used for them are simply available to be used for other things. The values in them will remain there until they happen to get overwritten when and if that memory gets used for something else. If you have a large local variable, such as a large array, it's very likely that a good portion of the data in that array will remain there even after the program ends.

I still don't fully understand about heap memory. is it static memory? like a static local variable that is initialized only once. static variable is not initialized and destroyed on each function call
Heap memory is just a big block of memory that the operating system allocates to your program. Your program has a memory manager that keeps track of which chunks of that memory have been allocated via functions such as malloc().

Imagine that heap memory is like a hotel with thousands of rooms. Someone comes in and says that they need 15 rooms, you find fifteen rooms that are all together and tell them that they can use room 1340. They know that they can actually use rooms 1340 through 1354. You make a note that someone has 15 rooms starting at 1340 so that if someone else comes in asking for rooms you don't give them any of those. Later, the person comes back and says that they are done with 1340. You look at your list and see that whomever got 1340 got 15 rooms starting there, so you now know that all fifteen of those rooms are free and some or all of them can be given to someone else. But you don't do any housekeeping in this hotel -- whatever garbage the person that used those rooms left will still be there when the next person that comes in gets it and it is there responsibility to do any cleaning that is needed.
 
Top