c - Freeing up memory allocated for an arbitrary position at an array -


i writing code manage insertions , deletions array elements. know linked list or other data structure better suited this, i'm limited using array.

extra memory array allocations managed use of realloc() function. however, deletions, must free memory allocated last element. this, tried using free (a + n), n representing last element, however, ends giving crashes like:

*** error in `./a.out': munmap_chunk(): invalid pointer:  0x000000000158f018 *** aborted (core dumped) 

i believe crashes expected, since os manages list of allocations heap, , while a has been allocated through malloc(), os not know allocations a + n, resulting in hard error.

how should go around freeing last element stored in array?

use realloc shrink allocation:

array_size--; array = realloc(array, array_size); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -