【发布时间】:2015-02-19 05:35:01
【问题描述】:
我想了解更多关于堆上发生的事情。所以我看下面的C代码。它基本上只是在堆上为两个变量分配内存:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
char *char_ptr;
int *int_ptr;
int mem_size;
if(argc < 2)
mem_size = 50;
else
mem_size = atoi(argv[1]);
printf("\t[+] allocating %d bytes of memory on the heap for char_ptr\n", mem_size);
char_ptr = (char *) malloc(mem_size);
if(char_ptr == NULL)
{
fprintf(stderr, "Error: could not allocate heap memory. \n");
exit(-1);
}
strcpy(char_ptr, "This is memory located on the heap.");
printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);
printf("\t[+] allocating 12 bytes of memory on the heap for int_ptr\n");
int_ptr = (int * ) malloc(12);
if(int_ptr == NULL)
{
fprintf(stderr, "Error: could not allocate heap memory.");
exit(-1);
}
*int_ptr = 31337;
printf("int_ptr (%p) --> %d\n", int_ptr, *int_ptr);
printf("\t[-] freeing char_ptr's heap memory...\n");
free(char_ptr);
printf("\t[+] allocating another 15 bytes for char_ptr\n");
char_ptr = (char *) malloc(15);
if(char_ptr == NULL)
{
fprintf(stderr,"Error: could not allocate heap memory.\n");
exit(-1);
}
strcpy(char_ptr, "new memory");
printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);
free(int_ptr);
free(char_ptr);
}
此代码的输出如下所示:
[+] allocating 50 bytes of memory on the heap for char_ptr
char_ptr (0x8827008) --> 'This is memory located on the heap.'
[+] allocating 12 bytes of memory on the heap for int_ptr
int_ptr (0x8827040) --> 31337
[-] freeing char_ptr's heap memory...
[+] allocating another 15 bytes for char_ptr
char_ptr (0x8827050) --> 'new memory'
所以我猜 char_ptr 是指向分配内存的开头(0x8827008),对吧?
由于分配了 50 个字节,因此该内存的末尾应指向地址 0x882702A。下一个内存分配从地址 0x8827040 开始。我的问题是:为什么 int_ptr 不指向 0x882702B (第一次内存分配后的下一个地址)?或者换句话说:0x772702A 和 0x8827040 之间的内存发生了什么?
【问题讨论】:
-
是什么让你认为它应该指向下一个地址?
malloc可以随意分配物品;它不是堆栈或凹凸分配器。 -
标准警告:请do not cast
malloc()的返回值。 -
@SouravGhosh 感谢您提供的信息!
标签: c memory-management heap-memory