I have a global array of strings with undefined lengths:
static char *array[1<<8];
Those strings will be replaced by other strings, with probably a different size.
Every time a new string comes in and needs to be stored in the array, I have to do this:
free(array[i]);
array[i] = malloc(len);
strcpy(array[i], str);
Now it could happen that the old and the new string are very similar in size, and memory could just be reused.
I believe that realloc()
does reuse the allocated memory directly in such circumstances.
Is there a standard function like realloc()
that does not copy the data?
Or does does it even mater performance wise, because of the free()
/malloc()
are somehow smart enough to reuse the memory of the latest free()
directly if it fits?
I've found that. But the responses are not satisfying for me.
Why is there no function in standard C library like realloc() without data copying?
question from:
https://stackoverflow.com/questions/66067351/repurposing-allocated-memory 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…