Memory layout in c

Thread Starter

@vajra

Joined May 2, 2018
150
considering that the memory is divided into four segments: stack heap, code and data,
1576649552084.png


I think they would be allocated as follows:

  • Global variables ------->
  • Local variables -------> stack
  • Static Global variables -------> stack
  • Static Local variables -------> stack
  • Constant data types ----->
  • Pointer -------->
  • Register -----> CPU register
  • Pointers (for example, char *arr, int *arr) -------> heap
  • Dynamically allocated space (using malloc and calloc) --------> stack
 

WBahn

Joined Mar 31, 2012
25,115
While there are some things that can be said that are generally true, for most of this it is entirely up to the compiler writer to make these decisions. If you look in the language standard, for instance, the word "stack" doesn't even appear (in the draft standard for C99).

It also depends on the OS and the version of the OS, as each defines a function calling convention and some of these pass everything on the stack and others pass the first few arguments via registers.

Having said that, why would you think that dynamically allocated memory would be placed on the stack?

Why do you think that pointers would all be placed in the heap?
 

Thread Starter

@vajra

Joined May 2, 2018
150
why would you think that dynamically allocated memory would be placed on the stack?
Why do you think that pointers would all be placed in the heap?
I made a mistake Pointer would placed in the stack and dynamically allocated variable would placed in heap
 

Papabravo

Joined Feb 24, 2006
12,774
I made a mistake Pointer would placed in the stack and dynamically allocated variable would placed in heap
I don't think you can make any general statements about where things belong or should go to, As has already been mentioned it is up to the compiler and linkage editor where things get placed and how they are handled. the average programmer has no control over these things
 

MrChips

Joined Oct 2, 2009
19,931
I do a lot of embedded applications using large bitmaps, graphics screens, audio files, data files, battery backed user configuration data, etc.

I can place my variables wherever I choose. For example, I have a bitmap image stored at some defined address:

uint8_t image_buffer[IMAGE_BUFFER_SIZE] @ IMAGE_BUFFER_STARTING_ADDRESS;

Then again, I am not your average programmer.
 

Thread Starter

@vajra

Joined May 2, 2018
150
I can place my variables wherever I choose. For example, I have a bitmap image stored at some defined address:
I had read that we write storage specifier in front of variable, that means we are telling to the compiler that where we want to store variable in memory.

Storage specifier tells to compiler what is the life time of Variable, and where it would be placed in memory.
 

djsfantasi

Joined Apr 11, 2010
5,962
And let’s assuming that is correct. Then it is up to the compiler where is actually is placed. As others have mentioned, you have no idea.
 

Papabravo

Joined Feb 24, 2006
12,774
I had read that we write storage specifier in front of variable, that means we are telling to the compiler that where we want to store variable in memory.

Storage specifier tells to compiler what is the life time of Variable, and where it would be placed in memory.
The storage specifier specifies whether the object has internal, external, or no linkage, and whether it is in memory or a register if available. None of this has anything to do with where things actually end up. If the CPU has no registers how is the compiler going to put something "in a register". Conversely if the CPU has no data memory, then every piece of data goes in a register. As a programmer you like to think you are in control, but I assure you, that is not the case.
 
Last edited:
Top