Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C Program Memory Layout in Embedded Systems

Elctro1

New Member
Hi,
I’ve been learning about the memory layout in C programs for embedded systems, particularly the Text, Data, BSS, Stack, and Heap sections, and how variables are allocated to each.

However, I’m unsure about one key point:
Is this memory layout universal, or does it vary depending on the microcontroller, compiler?

From my understanding, this should be general concept, but the actual placement, sizes, and even the presence of certain sections may differ across platforms. I’d appreciate confirmation if this interpretation is correct
 
There may be similarities in the layouts, but each hardware architecture will have unique features. For example, some devices begin execution at a low address while others use a high address. There are even examples of beginning addresses the are in between low and high. The operation of stack pointers may decrease when putting items onto a stack and increase when removing them. It could also be the reverse.
 
There may be similarities in the layouts, but each hardware architecture will have unique features. For example, some devices begin execution at a low address while others use a high address. There are even examples of beginning addresses the are in between low and high. The operation of stack pointers may decrease when putting items onto a stack and increase when removing them. It could also be the reverse.
Thanks. I'm trying to understand how compilers assign different variables to memory sections in these architectures:

For 8051 (Harvard): Keil

  • Where do these typically get placed?
    • const variables
    • Global variables
    • Static variables
      Does the compiler automatically put them in .text, .data, .bss, stack or heap based on their type/size?
For ARM Cortex-M : GCC
Same questions:
  • Where do const, global and static variables usually go?
  • Is the assignment to .text, .data, .bss, stack or heap automatic based on variable type?
Is this mainly controlled by the compiler, or do we need to specify locations manually?
 
Generally, allocation of memory areas is automatic, unless you specifically override it.
If you are using an assembler, they you may have to define some areas.

None of the compilers I've used in the last few years have needed any manual settings for different data areas, though you can override such as the default heap size etc.

In the ones I use, static and const share the same area as global, the difference is in how the compiler treats them or permits access during compilation.

Local variables are usually on the stack. Heap is memory allocated at run-time, eg. malloc()

.bss is the "data" portion of the executable file, constants before they are copied to RAM variables.
.text is, rather unintuitively, the executable / compiled / machine code section.
 

Latest threads

Back
Top