Reputation: 5257
If i do the following with malloc
HashTableEntry *util = malloc(sizeof(HashTableEntry) * T->capacity);
all works well. However if i do the following with realloc
HashTableEntry *util = realloc(util, sizeof(HashTableEntry) * T->capacity);
realloc
returns NULL
. I also get a warning by gcc, saying that i haven't initialized util
before using it.
But, the following works perfectly well, like malloc
HashTableEntry *util = NULL;
util = realloc(util, sizeof(HashTableEntry) * T->capacity);
My question is why?
What i believe is happening is that, in all cases, i am creating a pointer and assigning to it the starting address of a block of heap memory i am allocating. Am i wrong somewhere?
Upvotes: 0
Views: 1206
Reputation: 16540
Some items to note:
When calling realloc()
do NOT assign directly into the target pointer. Rather assign to a temporary pointer, then check (!=NULL) before assigning to the target pointer.
Then, if/when realloc()
fails, the original pointer will not be lost. Losing such a pointer results in a memory leak.
Note: the first parameter of realloc()
is a 'source' pointer. It must be pointing to something meaningful, like NULL
or a known place in the heap.
Upvotes: 0
Reputation: 301
If the first argument is a null pointer, realloc()
behaves like malloc() for the specified size.
If the first arg is an uninitialized pointer (bad pointer), realloc()
won't work properly, so it returns NULL.
If the first arg is a valid address, realloc()
deallocates the old object and returns a pointer to a new object or it expands the memory of the old object (it's up to the OS to decide).
Upvotes: 3
Reputation: 8579
In the second case util
is uninitialised when passed into realloc()
.
So the behaviour of realloc()
is undefined.
HashTableEntry *util = realloc(util, sizeof(HashTableEntry) * T->capacity);
//^^^^ This value is undefined.
In the third case you initialise it to NULL
in which case realloc()
behaves like malloc()
.
Upvotes: 1
Reputation: 87386
In general, functions look at the values of their arguments and use the values to do something, so it's important to pass a specific value instead of something random or unspecified. When you write HashTableEntry *util = realloc(util, sizeof(HashTableEntry) * T->capacity);
, you haven't told the compiler what value to put in the util
variable when it calls the realloc
function, so you'll probably get undefined behavior, which is bad.
Upvotes: 3
Reputation: 310920
As it is seen from the function call
HashTableEntry *util = realloc(util, sizeof(HashTableEntry) * T->capacity);
the function realloc
uses argument util
. If it has an indeterminate value then the function has undefined behavior.
Also before using any function you should read its description and the purpose of each its parameter.
From the C Standard (7.22.3.5 The realloc function)
2 The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size....
and
3 If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.
Upvotes: 1