1. 程式人生 > 其它 >C語言 記憶體佈局

C語言 記憶體佈局

執行結果截圖:

可以將執行結果截圖比對如下的記憶體佈局表:

程式碼:

#include "stdio.h"
#include "stdlib.h"

int global_uninit_var;
int global_init_var1 = 520;
int global_init_var2 = 880;

void func(void);

void func(void)
{
;
}

int main()
{
int local_var1;
int local_var2;

static int static_uninit_var;
static int static_init_var = 456;

char * str1 = "I love Johnny's Blog garden!";
char * str2 = "You are right!";

int * malloc_var = (int *)malloc(sizeof(int));
if(NULL == malloc_var)
{
exit(1);
}

printf("addr of local_var1 -> %p\n", &local_var1);
printf("addr of local_var2 -> %p\n", &local_var2);
printf("addr of malloc_var -> %p\n\n", malloc_var);
printf("addr of global_uninit_var -> %p\n", &global_uninit_var);
printf("addr of static_uninit_var -> %p\n", &static_uninit_var);
printf("addr of static_init_var -> %p\n", &static_init_var);
printf("addr of global_init_var2 -> %p\n", &global_init_var2);
printf("addr of global_init_var1 -> %p\n", &global_init_var1);
printf("addr of str2 -> %p\n", str2);
printf("addr of str1 -> %p\n", str1);
printf("addr of func -> %p\n", func);

free(malloc_var);

return 0;
}