In C (or C++) I'm wondering if it's possible to partially deallocate a block of memory.
For example, suppose we create an array of integers a
of size 100,
int * a = malloc(sizeof(int)*100);
and then later we want to resize a
so that it holds 20 ints rather than 100.
Is there a way to free only the last 80*sizeof(int) bytes of a
? For example if we call realloc, will it do this automatically?
- I'm looking for a solution that doesn't require moving/copying the first 20 ints.
- Alternatively, can you explain either why it would be bad if this were possible, or why the ability to do this wasn't included in either language?