Does a pointer variable need to be freed before using realloc?

Do I need to free memory before realloc for a pointer variable. Which of the following is true?

 for(i = 0; i < n; i++){ myArray = (int *)realloc(myArray, i*sizeof(int)); } for(i = 0; i < n; i++){ myArray = (int *)realloc(myArray, i*sizeof(int)); free(myArray); myArray = NULL; } 
+4
source share
4 answers

The specific usefulness of realloc is that before using < not <<< 21>, it must exist to expand the allocated memory.

So this is not required and will be unusual. When a NULL pointer is passed, realloc behaves like a malloc . If you use free before calling it, you can use malloc .

None of the examples are correct, since you did not specify error handling. All allocators can return NULL , and using realloc bit more complicated in this regard. Read the documents and examples carefully. In particular, ptr = realloc(ptr, ... always a bad idea, because if realloc fails and returns NULL , you just lost your link and leaked into memory. Instead, use the tmp variable, for example:

 tmp = realloc(ptr, newSize); if (tmp != NULL) ptr = tmp; else handle_error(); 
+11
source

No one. You should always check that realloc returns NULL; if you do not, you will be memory leaks if this happens. In addition, you have chosen the return value of the distribution function, impractical . Also, it is better to use sizeof(*myArray) instead of the explicit sizeof(type) construct, because if you later change the type of your pointer to an array / pointer, and you also forget to change the type here, you will run into difficulties - fixing segfault and / or memory. Summarizing:

 for(i = 0; i < n; i++){ int *tmp; tmp = realloc(myArray, i*sizeof(*myArray)); if (tmp == NULL) { free(myArray); /* And handle error */ } myArray = tmp; } 

And good to answer your question: you do not need to free ().

+5
source

It depends on what you want to do. realloc() specifically designed to accept an existing distribution and resize it, storing as much data as possible. Both of the examples you provided are true in the sense that they will compile, but they will do different things.

realloc(NULL, n) same as malloc(n) , so the second case is semantically equivalent:

 for(i = 0; i < n; i++){ myArray = (int *)malloc(i*sizeof(int)); free(myArray); myArray = NULL; } 

In the first example, the data pointed to by myArray will be saved. In the second example, the existing distribution will be discarded and replaced with a new, uninitialized distribution. Any data indicated by the original distribution will be discarded.


It should be noted that realloc() will return NULL if the distribution fails, but if you passed it a pointer that is not NULL , then this distribution will not be freed. Passing one pointer to realloc() and assigning the result directly to the same pointer variable can lead to a memory leak if the redistribution fails, since the original distribution will still exist. The proper way to do this is to use a temporary pointer variable.

+4
source

Not. What for? The whole point of realloc is that it reallocates an existing block of memory, preserving the values ​​already stored in that block of memory. The idea is that in some cases it can reuse an existing memory location instead of allocating a completely new memory block and copying the old values ​​there.

If you do not want to keep the contents of the old block, you may not need realloc at all.

It should also be noted that the realloc capabilities also encompass the malloc and free functionality, given the corresponding argument values. What you do in the second code loop essentially uses realloc in pure malloc mode.

+2
source

All Articles