Where Do C Arrays Live in Memory? Stack, Global, and Heap Explained
This tutorial shows how C arrays are stored in the stack, global segment, and heap on a 64‑bit Linux system, using gcc compilation, gdb inspection, and concrete memory addresses to illustrate the exact layout of each region.
Array in the Stack
We start with a simple function that declares a local integer array int arr[6]; and fills it with values 100‑600. The code is compiled with gcc -g -fno-stack-protector a.c to disable stack canaries, making the generated assembly easier to read.
void arr_on_stack() {
int arr[6];
arr[0]=100;
arr[1]=200;
arr[2]=300;
arr[3]=400;
arr[4]=500;
arr[5]=600;
int a = arr[0];
}Using gdb we disassemble the function and see that each element is stored at an offset from the base pointer rbp. For example, the instruction movl $0x64,-0x20(%rbp) stores the value 100 at rbp‑0x20. The base pointer at runtime is 0x7ffffffee2a0, so the array starts at 0x7ffffffee280. Verifying with p &arr confirms the address.
The stack representation of the array is therefore the contiguous memory region from 0x7ffffffee280 to 0x7ffffffee298.
Array in the Global Segment
A global array is declared as int global_array[6]; and initialized inside a function. After compiling with the same flags, gdb shows the address of the global array as 0x601050 (symbol <global_array>).
int global_array[6];
void arr_on_global() {
global_array[0]=1;
global_array[1]=2;
global_array[2]=3;
global_array[3]=4;
global_array[4]=5;
global_array[5]=6;
int b = global_array[0];
}Inspecting the memory with x/6wd 0x601050 prints the six integers in order, confirming that the global array occupies the contiguous region 0x601050 – 0x601068.
Array in the Heap
When an array is allocated dynamically with malloc, the pointer returned points to heap memory. The example allocates six integers, stores the same values, and then reads the first element.
void array_on_heap() {
int* arr = (int*)malloc(sizeof(int) * 6);
arr[0] = 100;
arr[1] = 200;
arr[2] = 300;
arr[3] = 400;
arr[4] = 500;
arr[5] = 600;
int a = arr[0];
}Setting a breakpoint at int a = arr[0]; and printing p arr yields the heap address 0x602010. Using x/6wd 0x602010 shows the stored values, confirming that the heap array occupies the region 0x602010 – 0x602028.
In all three cases, a C array is simply a contiguous block of memory; the only difference is where that block resides: the stack for local automatic variables, the global segment for static storage, and the heap for dynamically allocated memory.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
