b = realloc(a, 1000);
if realloc succeeds and b!=a (not in-place replacement), does realloc automatically free a or I should free both a and b afterwards?
thank you!
b = realloc(a, 1000);
if realloc succeeds and b!=a (not in-place replacement), does realloc automatically free a or I should free both a and b afterwards?
thank you!
If realloc succeeds, you don't need to free a, just b.
surely, but i just want to know what happens with a.a gets freed automatically after realloc succeeds? i didnt find this information anywhere and I was just curious...
If it has to move the memory, the original memory block will be freed for you.
thank you very much!
Note that with the null pointer behavior of realloc, you do not need malloc. This can make for a very clean loop to create and resize a buffer.
Just be careful who you share the a pointer value with if you plan to realloc, this is not JAVA!