[英]How to get the right start addrss of heap in Linux?
In my C Code: 在我的C代码中:
unsigned char* p2 = NULL;
p2 = malloc(0x1024);
printf("start of heap = %p \r\n",p2);
printf("end of heap = 0x%x \r\n",(unsigned long)sbrk(0));
unsigned long sizeOfHeap = (unsigned long)sbrk(0) - (unsigned long)p2;
printf("sizeOfHeap = 0x%x \r\n",sizeOfHeap);
And it shows: 它显示:
yangxiaoyu@STB-240:~/test/c/mem$ ./a.out
start of heap = 0x1b26010
end of heap = 0x1b48000
sizeOfHeap = 0x21ff0
But when I add "while(1)" at the end of this code, and cat proc maps, 但是,当我在此代码的末尾添加“ while(1)”并添加cat proc映射时,
yangxiaoyu@STB-240:~/test/c/mem$ ./a.out &
[1] 163026
yangxiaoyu@STB-240:~/test/c/mem$ start of heap = 0xb7b010
end of heap = 0xb9d000
sizeOfHeap = 0x21ff0
yangxiaoyu@STB-240:~/test/c/mem$ cat /proc/163026/map
map_files/ maps
yangxiaoyu@STB-240:~/test/c/mem$ cat /proc/163026/map
map_files/ maps
yangxiaoyu@STB-240:~/test/c/mem$ cat /proc/163026/maps
00400000-00401000 r-xp 00000000 08:12 266873328 /home/yangxiaoyu/test/c/mem/a.out
00600000-00601000 r--p 00000000 08:12 266873328 /home/yangxiaoyu/test/c/mem/a.out
00601000-00602000 rw-p 00001000 08:12 266873328 /home/yangxiaoyu/test/c/mem/a.out
00b7b000-00b9d000 rw-p 00000000 00:00 0 [heap]
7f499604e000-7f499620c000 r-xp 00000000 08:03 9572117 /lib/x86_64-linux-gnu/libc-2.19.so
7f499620c000-7f499640c000 ---p 001be000 08:03 9572117 /lib/x86_64-linux-gnu/libc-2.19.so
7f499640c000-7f4996410000 r--p 001be000 08:03 9572117 /lib/x86_64-linux-gnu/libc-2.19.so
7f4996410000-7f4996412000 rw-p 001c2000 08:03 9572117 /lib/x86_64-linux-gnu/libc-2.19.so
7f4996412000-7f4996417000 rw-p 00000000 00:00 0
7f4996417000-7f499643a000 r-xp 00000000 08:03 9572106 /lib/x86_64-linux-gnu/ld-2.19.so
7f4996624000-7f4996627000 rw-p 00000000 00:00 0
7f4996638000-7f4996639000 rw-p 00000000 00:00 0
7f4996639000-7f499663a000 r--p 00022000 08:03 9572106 /lib/x86_64-linux-gnu/ld-2.19.so
7f499663a000-7f499663b000 rw-p 00023000 08:03 9572106 /lib/x86_64-linux-gnu/ld-2.19.so
7f499663b000-7f499663c000 rw-p 00000000 00:00 0
7ffde625a000-7ffde627b000 rw-p 00000000 00:00 0 [stack]
7ffde633b000-7ffde633d000 r--p 00000000 00:00 0 [vvar]
7ffde633d000-7ffde633f000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
yangxiaoyu@STB-240:~/test/c/mem$
you can get 你可以得到
start of heap = 0xb7b010 , 堆开始= 0xb7b010 ,
and 和
00b7b000-00b9d000 rw-p 00000000 00:00 0 [heap] 00b7b000-00b9d000 rw-p 00000000 00:00 0 [堆]
so, How I get the real address of heap in C code? 那么,如何在C代码中获得堆的真实地址? WHY the address of malloc(0xb7b010) is not equal to 0b7b000 ? 为什么malloc(0xb7b010)的地址不等于0b7b000?
00b7b000
is the actual start of heap memory allocated to the program. 00b7b000
是分配给程序的堆内存的实际开始。
When you call malloc
it will store some data before the pointer returned (in this case 16 bytes). 当您调用malloc
,它将在指针返回之前存储一些数据(在本例中为16个字节)。 The data stored is implementation specific, but at least size of memory allocated should be stored. 存储的数据是特定于实现的,但至少应存储分配的内存大小。
This information is used when you call free
to properly free the memory for future use. 当您拨打free
电话以适当释放内存以备将来使用时,将使用此信息。 It is also used for realloc
functions. 它也用于重新realloc
函数。
As for the end not matching the given size, probably the program takes a chunk of memory from the OS for heap and uses it as needed. 至于末尾与给定大小不匹配,程序可能会从操作系统中获取一块内存作为堆,并根据需要使用它。 So, even though you have allocated 0x21ff0
bytes some more data is still available to allocate before asking the OS for more memory. 因此,即使您分配了0x21ff0
字节,在向OS请求更多内存之前,仍有更多数据可分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.