C Concepts Memory

Thread Starter

Kittu20

Joined Oct 12, 2022
511
I’m trying to understand the C memory layout and would appreciate your feedback on my general interpretation.

Assuming a memory range from 0x00 to 0xFF, memory is typically divided into the following segments:

  1. Text Segment (0x00 - 0x3F): Contains the program’s compiled instructions.
  2. Data Segment:
    • Initialized Data (0x40 - 0x6F): For global/static variables with initial values.
    • BSS (0x70 - 0x7F): For uninitialized global/static variables.
  3. Stack (0x80 - 0xBF): Manages function calls, local variables, and return addresses.
  4. Heap (0xC0 - 0xFF): Used for dynamic memory allocation.

To inspect where static and global variables are stored, I understand we can check both the object file and the map file generated during compilation. However, stack and heap information can only be observed during runtime.

Could you please confirm if this understanding is correct or suggest any adjustments?

Note : This is a general question, and I’m looking for a general answer. Once I’m on the right track, I plan to ask more specific questions regarding memory inspection with the GCC compiler on Ubuntu.
 

BobTPH

Joined Jun 5, 2013
11,515
My memory may be wrong, but didn’t you ask the same question a couple of years ago?

Edited to add: Not exactly, I must be thinking if this.
 
Last edited:

Thread Starter

Kittu20

Joined Oct 12, 2022
511
My memory may be wrong, but didn’t you ask the same question a couple of years ago?

Edited to add: Not exactly, I must be thinking if this.
Yes, but I didn’t fully understand the concept before, I was badly confused between the memory model of a microcontroller and the memory model of a PC. so I’m trying again. This time, I am planning to experiment on a Linux machine.

Edit : I have generated the object file and map file on Linux, but before asking specific questions, I’m trying to figure things out on my own. I’m just looking for confirmation from an experienced person that my thinking is correct. Once I have a more specific question, I’ll reach out.
 
Last edited:

WBahn

Joined Mar 31, 2012
32,823
You are asking questions that have nothing to do with the C language. They are implementation details that are up to the implementer of the compiler and are driven by the operating environment that program will be executed on.

If you look in the C language specification, the words "heap" and "stack" do not appear.

The language specification provides required behaviors, it is entirely up to the compiler implementation to find a way to provide those behaviors.
 
Top