Search code examples
c++memory-managementdynamic-memory-allocation

reallocating memory for an array c++


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 :)


Solution

  • Only if the memory for the array was allocated via malloc(), calloc(), or realloc() earlier. Otherwise you're asking for trouble.