I need to know, are there any risks using the realloc() function to get additional memory?
For instance , I’m storing some stream into a char* variable. The problem is that we can’t specify at the beginning the maximum size of the input stream, so we need from time to time to expand the memory area.
My question is: could reallocating memory cause data loss of my reallocated variable?
Can I lose data if I use it correctly?
No, there should never be any data loss when using
realloc, it should be the syntactically the same as doing the below.What if reallocation fails?
If the reallocation failed (ie. no new memory could be allocated) the pointer to
old_memorywill still be valid, so please don’t do like this:What does the standard say about
realloc?