-
Notifications
You must be signed in to change notification settings - Fork 10
/
memory_segments.c
executable file
·35 lines (25 loc) · 1.08 KB
/
memory_segments.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
int global_var;
int global_initialized_var = 5;
void function() { // This is just a demo function
int stack_var; // notice this variable has the same name as the one in main()
printf("the function's stack_var is at address 0x%08x\n", &stack_var);
}
int main() {
int stack_var; // same name as the variable in function()
static int static_initialized_var = 5;
static int static_var;
int *heap_var_ptr;
heap_var_ptr = (int *) malloc(4);
// These variables are in the data segment
printf("global_initialized_var is at address 0x%08x\n", &global_initialized_var);
printf("static_initialized_var is at address 0x%08x\n\n", &static_initialized_var);
// These variables are in the bss segment
printf("static_var is at address 0x%08x\n", &static_var);
printf("global_var is at address 0x%08x\n\n", &global_var);
// This variable is in the heap segment
printf("heap_var is at address 0x%08x\n\n", heap_var_ptr);
// These variables are in the stack segment
printf("stack_var is at address 0x%08x\n", &stack_var);
function();
}