数据在计算机内部的存储位置
代码全览
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
static void show_pointer(void *p
, char *descr
) {
printf("%s\t%p\t%lu\n", descr
, p
, (unsigned long) p
);
}
char big_array
[1L<<24];
char huge_array
[1L<<30];
int global
= 0;
int useless() { return 0; }
int main
()
{
void *p1
, *p2
, *p3
, *p4
;
int local
= 0;
p1
= malloc(1L << 28);
p2
= malloc(1L << 8);
p3
= malloc(1L << 16);
p4
= malloc(1L << 8);
show_pointer((void *) big_array
, "big array");
show_pointer((void *) huge_array
, "huge array");
show_pointer((void *) &local
, "local");
show_pointer((void *) &global
, "global");
show_pointer((void *) p1
, "p1");
show_pointer((void *) p2
, "p2");
show_pointer((void *) p3
, "p3");
show_pointer((void *) p4
, "p4");
show_pointer((void *) useless
, "useless");
show_pointer((void *) exit
, "exit");
show_pointer((void *) malloc
, "malloc");
return 0;
}
运行结果
说明
malloc 函数和 free函数存在栈里 函数参数和局部变量存储在栈里 全局变量和静态变量存储在数据段里
转载请注明原文地址: https://yun.8miu.com/read-134943.html