S
Snis Pilbor
Howdy C warriors =)
I'd like to be able to malloc a block of memory, then later free just a
certain (trailing or leading) portion of it. For example, malloc 100
bytes, then later free the last 20 of them. If I just blindly do
something like free(myptr + 80), will this:
1. be guaranteed to do what I want and safely free the last 20 bytes,
2. be guaranteed to corrupt memory, or
3. have behavior dependent on implementation and not nailed down by C
specifications?
If 2 or 3, is there some other way to do what I want? For example some
way of telling it to malloc 80 bytes, then 20, but specify that the 20
must start where the 80 end?
BTW, the reason I want to do this is I am computing a linked list of
structures for later usage. During the computation stage, each element
will require some extra storage for computations, but once computation
is done and the list is prepared, this extra storage will no longer be
needed. Yes, I could maintain a separate and parallel linked list for
just the calculation storage, but this would be a big pain.
Thanks =)
I'd like to be able to malloc a block of memory, then later free just a
certain (trailing or leading) portion of it. For example, malloc 100
bytes, then later free the last 20 of them. If I just blindly do
something like free(myptr + 80), will this:
1. be guaranteed to do what I want and safely free the last 20 bytes,
2. be guaranteed to corrupt memory, or
3. have behavior dependent on implementation and not nailed down by C
specifications?
If 2 or 3, is there some other way to do what I want? For example some
way of telling it to malloc 80 bytes, then 20, but specify that the 20
must start where the 80 end?
BTW, the reason I want to do this is I am computing a linked list of
structures for later usage. During the computation stage, each element
will require some extra storage for computations, but once computation
is done and the list is prepared, this extra storage will no longer be
needed. Yes, I could maintain a separate and parallel linked list for
just the calculation storage, but this would be a big pain.
Thanks =)