Just started to learn C and came across the following issue:
I need to shrink an integer array in C, removing elements at the end. By removing I mean freeing. The common answer is to allocate new memory for the smaller array, after which to copy all items ( -items to remove ) of the original array into the newly allocated memory, and then free() the original array.
Because I must deal with very large arrays, I'd rather skip the copying part.
Would it be possible to create a pointer variable that points to "near the end of the original array" of size "end of array - near the end", and then free that pointer?
Thanks in advance
realloc
. It's not guaranteed but some implementations will actually give you back the original pointer you passed, without copying anything. – Leaseholderrealloc
? – Grinderfree()
to release part of a memory block by passing a pointer "near the end". That would almost certainly cause a crash or heap corruption. – Tomfool