Reputation: 4672
Friends
In our C++ , Iam current using realloc method to resize the memory allocated by malloc. realloc() usage is done as below
my_Struct *strPtr =(my_struct*)malloc(sizeof(my_Struct));
/* an later */
strPtr = (my_struct*)realloc(strPtr,sizeof(my_Struct)*NBR);
now wikipeadia (_http://en.wikipedia.org/wiki/Malloc)says that
If instead one did
void *p = malloc(orig_size);
/* and later... */
p = realloc(p, big_size);
then in case it is not possible to obtain big_size bytes of memory, p will have value NULL and we no longer have a pointer to the memory previously allocated for p, creating a memory leak
And it also says that the correct way to rectify the above error is
void *p = malloc(orig_size);
/* and later... */
void *tmp = realloc(p, big_size);
if (tmp != NULL)
{
p = tmp; /* OK, assign new, larger storage to p */
}
else
{
/* handle the problem somehow */
}
Can you tell me which is the best way to use realloc()
also once I have pointer to a structure and then while using realloc later can i use pointer to a void ???
Many Thanks
Upvotes: 2
Views: 3438
Reputation: 170549
Use the suggested approach – hold the pointer to the previous buffer until realloc has successfully returned. Once realloc() successfully returns the previous block has been freed and all pointers to it have become dangling – adjust them.
realloc as well as malloc don't care what is the pointer type - you can use void* as well as anything*.
Upvotes: 1
Reputation: 57066
Why are you using malloc and realloc? There's almost always a better way in C++.
If you're using it to make variable-length arrays, you're almost certainly better off with std::vector<>, or perhaps one of the other container templates.
If you're using C instead, perhaps with a C++-capable compiler, then the correct way is the second method, which doesn't lose an entire memory block when it fails to allocate.
The answer to your second question also depends on whether you're using C or C++. In C, void * is a generic data pointer type, and is freely convertible. In C++, void * needs to be explicitly converted. If you're actually writing C, you need to use malloc() and friends, which work on void *. If you're really writing C++, you need to cast, which makes it more awkward. In neither case does realloc() work on pointers to struct, but rather pointers to void.
Upvotes: 0
Reputation: 264679
When realloc() fails and returns NULL the original memory is untouched.
So you should use it like this:
my_Struct* strPtr =(my_struct*)malloc(sizeof(my_Struct));
/* an later */
my_Struct* tmp = (my_struct*)realloc(strPtr,sizeof(my_Struct)*NBR);
if (tmp != NULL)
{
strPtr = tmp;
}
else
{
/* realloc Failed. Need to do something */
}
Upvotes: 0
Reputation: 37238
Like others have said, just use realloc properly as suggested.
But the "C++ way" of doing this is really to use a std::vector<> rather than maintaining arrays yourself. That way the C++ standard library will take care of the low-level details of reallocation (presumably by using realloc()).
Upvotes: 0
Reputation: 3380
Malloc() and realloc() are C functions. Actually, realloc() does malloc() and free() depending on the arguments you pass:
Quoted from Here, where you have a deeper explanation.
The C library makes it impossible to expand a memory block in place, so C++ doesn't support it either.
If you want to stick to C functions, then you should hold the pointer of your first memory allocation when calling realloc(). Then you check if it is NULL, otherwise you assign it, just as you did in your latst code.
But maybe for C++ the best solution is to make your own mallocator, the std solution based on C's malloc(). Check this, or this.
Upvotes: 5
Reputation: 400079
Of course you must protect against the case that realloc()
returns NULL
. It is a memory allocation, and in C (where realloc()
) is mostly used, I think C++ programmers think it is a bit low-level/qaint to use raw realloc()
calls, memory allocations can always fail.
Directly overwriting the pointer with the return value is an error, as that drops the original pointer and makes the memory leak in case the reallocation failed.
Upvotes: 7