Defined in header <stdlib.h> | ||
void*realloc(void*ptr,size_t new_size); | ||
Reallocates the given area of memory. Ifptr is not NULL, it must be previously allocated bymalloc,calloc orrealloc and not yet freed with a call tofree orrealloc. Otherwise, the results are undefined.
The reallocation is done by either:
If there is not enough memory, the old memory block is not freed and null pointer is returned.
Ifptr isNULL, the behavior is the same as callingmalloc(new_size).
Otherwise,
ifnew_size is zero, the behavior is implementation defined (null pointer may be returned (in which case the old memory block may or may not be freed), or some non-null pointer may be returned that may not be used to access storage).Such usage is deprecated (viaC DR 400).(since C17) | (until C23) |
ifnew_size is zero, the behavior is undefined. | (since C23) |
A previous call tofree or | (since C11) |
Contents |
| ptr | - | pointer to the memory area to be reallocated |
| new_size | - | new size of the array in bytes |
On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated withfree orrealloc. The original pointerptr is invalidated and any access to it is undefined behavior (even if reallocation was in-place).
On failure, returns a null pointer. The original pointerptr remains valid and may need to be deallocated withfree orrealloc.
Originally (in C89), support for zero size was added to accommodate code such as
OBJ*p=calloc(0,sizeof(OBJ));// "zero-length" placeholder/*...*/while(1){ p= realloc(p, c*sizeof(OBJ));// reallocations until size settles/* code that may change c or break out of loop */}
#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <string.h> void print_storage_info(constint* next,constint* prev,int ints){if(next)printf("%s location: %p. Size: %d ints (%ld bytes).\n",(next!= prev?"New":"Old"),(void*)next, ints, ints*sizeof(int));elseprintf("Allocation failed.\n");} int main(void){constint pattern[]={1,2,3,4,5,6,7,8};constint pattern_size=sizeof pattern/sizeof(int);int*next=NULL,*prev=NULL; if((next=(int*)malloc(pattern_size*sizeof*next)))// allocates an array{memcpy(next, pattern,sizeof pattern);// fills the array print_storage_info(next, prev, pattern_size);}elsereturnEXIT_FAILURE; // Reallocate in cycle using the following values as a new storage size.constint realloc_size[]={10,12,512,32768,65536,32768}; for(int i=0; i!=sizeof realloc_size/sizeof(int);++i){if((next=(int*)realloc(prev= next, realloc_size[i]*sizeof(int)))){ print_storage_info(next, prev, realloc_size[i]);assert(!memcmp(next, pattern,sizeof pattern));// is pattern held}else// if realloc failed, the original pointer needs to be freed{free(prev);returnEXIT_FAILURE;}} free(next);// finally, frees the storagereturnEXIT_SUCCESS;}
Possible output:
New location: 0x144c010. Size: 8 ints (32 bytes).Old location: 0x144c010. Size: 10 ints (40 bytes).New location: 0x144c450. Size: 12 ints (48 bytes).Old location: 0x144c450. Size: 512 ints (2048 bytes).Old location: 0x144c450. Size: 32768 ints (131072 bytes).New location: 0x7f490c5bd010. Size: 65536 ints (262144 bytes).Old location: 0x7f490c5bd010. Size: 32768 ints (131072 bytes).
C++ documentation forrealloc |