3 ms·
actually, realloc infrequently moves data around, especially if it's large. quite often the returned pointer value is the same. but yes, even if it has been fre
by foxhill 12y ago
actually, realloc infrequently moves data around, especially if it's large. quite often the returned pointer value is the same. but yes, even if it has been free'd, the page in which it resided is likely still valid, and free doesn't zero out memory. so, dereferencing p even after realloc will likely point to a section of memory with the data that was previously there, in it.
that said, it is, quite rightly, a use-after-free 101 bug. i only mention because a lot of people see this behaviour, and it only explodes sometime later (either exceed page boundary in old data, or getting stale data later on..)
- hackcasual 12y agoI believe realloc must return the same memory spot if the size (and maybe type?) didn't change.
- ygra 12y agoThe type is irrelevant , as realloc gets a void* and a size. But the standard says nothing about that a resize has to happen in-place if possible (which would be the case if it gets smaller or remains the same size). There is no requirement for the case that the requested size is the same as the memory block had before. That being said, most realloc implementations will probably hand out the same pointer again if size <= old_size; it just makes sense to move only when needed. But that behaviour is not required.