#include<stdio.h> #include<stdlib.h> int main(void) { int *p1=0; int *p2=0; p1=calloc(20, sizeof(int)); p2=realloc(p1, 10*sizeof(int)); printf("\n"); printf("%p\n", p1); printf("%p\n", p2); printf("\n"); return 0; }
解决方案
10
后果就是可能造成数据丢失,假设用calloc分配20个字节,写入20个字节的数据,
然后用realloc重新分配为10个字节,则之前写入的20个字节数据可能丢失后10个字节的数据。
然后用realloc重新分配为10个字节,则之前写入的20个字节数据可能丢失后10个字节的数据。
10
看一下realloc的实现源码就明白了:
void *realloc(void *ptr, size_t size)
{
struct free_arena_header *ah;
void *newptr;
size_t oldsize;
if (!ptr)
return malloc(size);
if (size == 0) {
free(ptr);
return NULL;
}
/* Add the obligatory arena header, and round up */
size = (size + 2 * sizeof(struct arena_header) – 1) & ARENA_SIZE_MASK;
ah = (struct free_arena_header *)
((struct arena_header *)ptr – 1);
if (ah->a.size >= size && size >= (ah->a.size >> 2)) {
/* This field is a good size already. */
return ptr;
} else {
/* Make me a new block. This is kind of bogus; we should
be checking the following block to see if we can do an
in-place adjustment… fix that later. */
oldsize = ah->a.size – sizeof(struct arena_header);
newptr = malloc(size);
memcpy(newptr, ptr, (size < oldsize) ? size : oldsize);
free(ptr);
return newptr;
}
}
从代码看,假如新分配的newsize小于之前的oldsize,那么也只拷贝newsize这么多,拷贝完之后就释放掉原来的ptr,返回新的newptr。这样,就会造成数据的截断,但并不会所谓“自由区域”或“野指针”。
void *realloc(void *ptr, size_t size)
{
struct free_arena_header *ah;
void *newptr;
size_t oldsize;
if (!ptr)
return malloc(size);
if (size == 0) {
free(ptr);
return NULL;
}
/* Add the obligatory arena header, and round up */
size = (size + 2 * sizeof(struct arena_header) – 1) & ARENA_SIZE_MASK;
ah = (struct free_arena_header *)
((struct arena_header *)ptr – 1);
if (ah->a.size >= size && size >= (ah->a.size >> 2)) {
/* This field is a good size already. */
return ptr;
} else {
/* Make me a new block. This is kind of bogus; we should
be checking the following block to see if we can do an
in-place adjustment… fix that later. */
oldsize = ah->a.size – sizeof(struct arena_header);
newptr = malloc(size);
memcpy(newptr, ptr, (size < oldsize) ? size : oldsize);
free(ptr);
return newptr;
}
}
从代码看,假如新分配的newsize小于之前的oldsize,那么也只拷贝newsize这么多,拷贝完之后就释放掉原来的ptr,返回新的newptr。这样,就会造成数据的截断,但并不会所谓“自由区域”或“野指针”。