So is there a function in any library like what I want?
No there is not. How is the system supposed to extend the piece of memory if there isn't room ?
e.g. imagine you do this:
char *a = malloc(2);
char *b = malloc(2);
The memory allocated might now look like:
1 2 3 4 5 6 7
-------------------------
| | | | | | |
-------------------------
\ /\ /\ /
\ / \ / \ /
v v v
memory unused/ memory
for "a" internal for "b"
memory piece
Now you do realloc(a,10);
. The system can't simply extend the memory piece for "a". It'll hit the memory used by "b", so instead it has to find another place in memory that has 10 contiguous free bytes , copy over the the 2 bytes from the old memory piece and return you a pointer to these new 10 bytes.
free
/malloc
the buffer if you don't care about the old contents? – Beemanmalloc()
. It is redundant at best, and may hide an error the compiler would have caught in its absence. – Thundersquallinsert
function for a. dynamic array, avoiding double copying – Headman