Re: realloc shrink memory
- From: David Schwartz <davids@xxxxxxxxxxxxx>
- Date: Fri, 17 Sep 2010 02:11:13 -0700 (PDT)
On Sep 16, 11:07 pm, w...@xxxxxxxxxxx wrote:
Most realloc(void* ptr, size_t size) documents say that realloc
changes the size of ptr pointed memory block. But all the
following descriptions basically says the returned value is a
pointer to a new memory block.
Correct.
My question is that if realloc is called to shorten the allocated
memory, is it always hold that the return value is the same as the
argument ptr?
No. And there's no way you can even tell if you've asked to shorten
the allocated memory. For example, there's no reason an implementation
couldn't elect to round a requested size of 100 bytes up to an
allocated size of 128 bytes but a requested size of 90 bytes up to an
allocated size of 256 bytes. In this case, a request to change the
size to 90 bytes when it was previously requested as 100 bytes would
*not* be a request to shorten the allocated memory, only the
*guaranteed* allocated memory. (And I do in fact know 'realloc'
implementations that act this way based on heuristics.)
Otherwise, the first sentence is misleading.
const size_t BSize=100;
void *p=malloc(BSize);
void *p1=realloc(p,BSize/2);
assert(p==p1); // assured?
Not at all. For one thing, the smaller sized block may even come from
a completely different allocator.
DS
.
- Follow-Ups:
- Re: realloc shrink memory
- From: Keith Thompson
- Re: realloc shrink memory
- From: wij
- Re: realloc shrink memory
- References:
- realloc shrink memory
- From: wij
- realloc shrink memory
- Prev by Date: open all your social sites facebook,youtube,etc new proxy sites*
- Next by Date: Re: realloc shrink memory
- Previous by thread: Re: realloc shrink memory
- Next by thread: Re: realloc shrink memory
- Index(es):
Relevant Pages
|