So, is there a function in any library, like what I want?
No no. How should the system distribute a piece of memory if there is no space?
eg. imagine that you are doing this:
char *a = malloc(2); char *b = malloc(2);
The allocated memory may now look like this:
1 2 3 4 5 6 7
-------------------------
| | | | | | |
-------------------------
\ / \ / \ /
\ / \ / \ /
vvv
memory unused / memory
for "a" internal for "b"
memory piece
You are now doing realloc(a,10); . The system cannot simply expand the memory fragment for "a". It will fall into the memory used by "b", so instead it should find another place in memory that has 10 contiguous free bytes, copy 2 bytes from the old part of the memory and return the pointer to these new 10 bytes.
nos
source share