What are the memory sections in a C program, and what goes where?
0 upvotes
Practice with AISoon
A typical embedded C program has these memory sections:
.text— compiled machine instructions, placed in flash (read-only)..rodata—constglobals, string literals, and lookup tables, also in flash..data— global and static variables with nonzero initializers. Their initial values are stored in flash and copied to RAM at boot by the C runtime..bss— global and static variables that are zero-initialized or have no explicit initializer. Zeroed at boot by the C runtime. No flash storage needed for values — only the section size.- Stack — local variables, function arguments, and return addresses. Grows downward from the top of RAM. Size is fixed at link time.
- Heap — dynamically allocated memory (
malloc/free). Grows upward. Many embedded systems avoid it entirely.
The critical interview point: only .data and .bss are initialized by the startup code. Local (stack) variables are NOT initialized — they contain whatever garbage was in that memory location. This is why int sum; for(...) sum += x; is a bug.
Source: C / C++ Concepts Q&A
