关于realloc的调整内存方式
PROTOTYPE:
void * realloc ( void * ptr, size_t new_size );
关于realloc的行为方式,结合源码总结为:
1. realloc失败的时候,返回NULL; 2. realloc失败的时候,原来的内存不改变,也就是不free或不move,(这个地方很容易出错); 3. 假如原来的内存后面还有足够多剩余内存的话,realloc的内存=原来的内存+剩余内存,realloc还是返回原来内存的地址; 假如原来的内存后面没有足够多剩余内存的话,realloc将申请新的内存,然后把原来的内存数据拷贝到新内存里,原来的内存将被free掉,realloc返回新内存的地址; 4. 如果size为0,效果等同于free(); 5. 传递给realloc的指针可以为空,等同于malloc; 6. 传递给realloc的指针必须是先前通过malloc(), calloc(), 或realloc()分配的。 |
source code在此,对号入座:
/* realloc.c - C standard library routine. Copyright (c) 1989, 1993 Michael J. Haertel You may redistribute this library under the terms of the GNU Library General Public License (version 2 or any later version) as published by the Free Software Foundation. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED WARRANTY. IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */ #include <limits.h> #include <stddef.h> #include <stdlib.h> #include <string.h> #include "malloc.h" #ifdef __ELF__ #pragma weak realloc = __libc_realloc #endif #define MIN(A, B) ((A) < (B) ? (A) : (B)) /* Resize the given region to the new size, returning a pointer to the (possibly moved) region. This is optimized for speed; some benchmarks seem to indicate that greater compactness is achieved by unconditionally allocating and copying to a new region. */ void * __libc_realloc (void *ptr, size_t size) { void *result, *previous; int block, blocks, type; int oldlimit; if (!ptr) return __libc_malloc(size); if (!size) { __libc_free(ptr); return __libc_malloc(0); } block = BLOCK(ptr); switch (type = _heapinfo[block].busy.type) { case 0: /* Maybe reallocate a large block to a small fragment. */ if (size <= BLOCKSIZE / 2) { if ((result = __libc_malloc(size)) != NULL) { memcpy(result, ptr, size); #if 1 __libc_free(ptr); #else _free_internal(ptr); #endif } return result; } /* The new size is a large allocation as well; see if we can hold it in place. */ blocks = BLOCKIFY(size); if (blocks < _heapinfo[block].busy.info.size) { /* The new size is smaller; return excess memory to the free list. */ _heapinfo[block + blocks].busy.type = 0; _heapinfo[block + blocks].busy.info.size = _heapinfo[block].busy.info.size - blocks; _heapinfo[block].busy.info.size = blocks; #if 1 __libc_free(ADDRESS(block + blocks)); #else _free_internal(ADDRESS(block + blocks)); #endif return ptr; } else if (blocks == _heapinfo[block].busy.info.size) /* No size change necessary. */ return ptr; else { /* Won't fit, so allocate a new region that will. Free the old region first in case there is sufficient adjacent free space to grow without moving. */ blocks = _heapinfo[block].busy.info.size; /* Prevent free from actually returning memory to the system. */ oldlimit = _heaplimit; _heaplimit = 0; #if 1 __libc_free(ptr); #else _free_internal(ptr); #endif _heaplimit = oldlimit; result = __libc_malloc(size); if (!result) { /* Now we're really in trouble. We have to unfree the thing we just freed. Unfortunately it might have been coalesced with its neighbors. */ if (_heapindex == block) __libc_malloc(blocks * BLOCKSIZE); else { previous = malloc((block - _heapindex) * BLOCKSIZE); __libc_malloc(blocks * BLOCKSIZE); #if 1 __libc_free(previous); #else _free_internal(previous); #endif } return NULL; } if (ptr != result) memmove(result, ptr, blocks * BLOCKSIZE); return result; } break; default: /* Old size is a fragment; type is logarithm to base two of the fragment size. */ if ((size > 1 << (type - 1)) && (size <= 1 << type)) /* New size is the same kind of fragment. */ return ptr; else { /* New size is different; allocate a new space, and copy the lesser of the new size and the old. */ result = __libc_malloc(size); if (!result) return NULL; memcpy(result, ptr, MIN(size, 1 << type)); __libc_free(ptr); return result; } break; } }
上面函数流程图如下:
注意事项:
1. ptr必须为NULL,或者为malloc,realloc或者calloc的返回值,否则发生realloc invalid pointer错误; 2. new_size如果小于old_size,只有new_size大小的数据会被保存,可能会发生数据丢失,慎重使用; 3. 如果new_size大于old_size,可能会分配一块新的内存,这时候ptr指向的内存会被释放,ptr成为野指针,再访问的时候会发生错误; 4. 最后不要将返回结果再赋值给ptr,即ptr=realloc(ptr,new_size)是不建议使用的,因为如果内存分配失败,ptr会变为NULL,如果之前没有将ptr所在地址赋给其他值的话,会发生无法访问旧内存空间的情况,所以建议使用temp=realloc(ptr,new_size)。流程图及注意事项来源于:http://www.cnblogs.com/ladd/archive/2012/06/30/2571420.html
Meet so Meet.
C plusplus
I-PLUS....