Reputation:
I was looking on C++ reference and found the dynamic memory management and was looking at the function that reallocate memory and I was wondering if this function could be used to make an array bigger I think that is what they mean but I really don't understand this line
more_numbers = (int*) realloc (numbers, count * sizeof(int));
this appears in the example on cplusplus reference so here is my question in a nutshell
can realloc()
be used to make an array bigger? thanks :)
Upvotes: 1
Views: 898
Reputation: 1274
"The function may move the memory block to a new location, in which case the new location is returned". Even though it preserves the data in your block, any pointers to the objects inside this block may become invalid.
Upvotes: 0
Reputation: 799450
Only if the memory for the array was allocated via malloc()
, calloc()
, or realloc()
earlier. Otherwise you're asking for trouble.
Upvotes: 8