After a successful allocation using malloc(), sometimes the program needs to increase the size of the array. Though malloc() worked successfully, but realloc() failed.
I've used this piece of code to handle the failure of realloc(). Here T is the template parameter. array is a pointer of type T, _size is the total index of the array has been already used, _maxSize is the maximum number of usable index. Reallocation is needed when _size reaches _maxSize.
if(_size == _maxSize){
_maxSize *= 2;
T *temp;
temp = (T *) realloc( array, sizeof( T ) * (_maxSize) ) ;
if(temp == NULL){
printf("...reallocation of memory failed...\n");
exit(0);
}
else{
array = temp;
}
}
Although the failure of realloc() was handled, the program really needs to increase the array size and I don't really know about how to perform the reallocation even if realloc() failed.