存入数据的地址与上一个数据的地址有关,并且会影响下一个数据的地址

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

static void show_pointer(void *p, char *descr) {
// printf(“Pointer for %s at %p\n”, descr, p);
printf("%s\t%p\t%lu\n", descr, p, (unsigned long) p);
}

char big_array[1L<<24]; /* 16 MB /
//char huge_array[1L<<31]; /
2 GB /
char huge_array[1L<<30];/
1 GB */
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 << 32);
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;

}

下面运行结果:
gec@ubuntu:/mnt/hgfs/share/csapp_code$ gcc locate.c
gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out
big array 0x4804a060 1208262752
huge array 0x804a060 134520928
local 0xbfcc9fdc 3217858524
global 0x804a044 134520900
p1 0xa7545008 2807320584
p2 0x49a67008 1235644424
p3 0x49a67110 1235644688
p4 0x49a77118 1235710232
useless 0x80484b6 134513846
exit 0x8048370 134513520
malloc 0x8048350 13451348

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值