realloc return value

S

subramanian

I have taken the following prototype from K & R.

void *realloc(void *p, size_t size);

Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
K & R 2nd edition says "realloc returns a pointer to the new space".

Why I am asking is that if they are different, the older pointer value
of p should be freed.

Thanks
 
J

jacob navia

subramanian a écrit :
I have taken the following prototype from K & R.

void *realloc(void *p, size_t size);

Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
K & R 2nd edition says "realloc returns a pointer to the new space".

Why I am asking is that if they are different, the older pointer value
of p should be freed.

Thanks

You do not have to free the old value. realloc will do it for you
 
R

Richard Heathfield

subramanian said:
I have taken the following prototype from K & R.

void *realloc(void *p, size_t size);

Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.

It *may* be different. That depends on whether realloc relocated the block
(and also on whether realloc succeeded). If it did relocate the block, p's
value is now indeterminate and should not be used. And if it failed,
realloc will return NULL. See below.
K & R 2nd edition says "realloc returns a pointer to the new space".

Why I am asking is that if they are different, the older pointer value
of p should be freed.

No, realloc will take care of that for you if it is necessary.

Note that realloc may *fail*, in which case it will return NULL. So don't do
this:

p = realloc(p, new_size); /* BAD DOG! NO BISCUIT! */

Instead, do this:

tmp = realloc(p, new_size);
if(tmp != NULL)
{
p = tmp;
tmp = NULL;
}
else
{
the reallocation failed, but at least p still points to the
old, untouched memory block, so you haven't *lost* any memory
}
 
M

mark_bluemel

subramanian said:
I have taken the following prototype from K & R.

void *realloc(void *p, size_t size);

Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.

We don't know, and we don't need to care. If it's not null, the
reallocation worked (and that could conceivably be by growing the
allocated memory in place, AFAIK), the old data has been (if necessary)
copied into the new location. If the old base address and the new base
address are different, the previously allocated space will have been
freed.
K & R 2nd edition says "realloc returns a pointer to the new space".
Why I am asking is that if they are different, the older pointer value
of p should be freed.

But not by you. That's realloc()'s job.
 
M

mark_bluemel

subramanian said:
I have taken the following prototype from K & R.

void *realloc(void *p, size_t size);

Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.

I've just checked with a copy of the draft C89 standard and it says
that the return pointer could be either...
 
I

indro

Da: jacob navia
Data: Mer 20 Dic 2006 13:20 ....
Da: Richard Heathfield
Data: Mer 20 Dic 2006 13:29
...
really strange:
jacob posted his message a couple of hours ago, and richard hadn't
"attaked" (harrassed ?) him ...
really strange
ah: got it ! they're disputating in the good ol' "size of pointers"
thread !
i would say: "they're not multithreaded" ... but the standard doesn't
know about threads
;-)
 
R

Richard Heathfield

indro said:
..
really strange:
jacob posted his message a couple of hours ago, and richard hadn't
"attaked" (harrassed ?) him ...

If he didn't screw up, why should I reply? What do you want me to say -
"Well done Mr Navia, you finally got something right"? Would not that, too,
be construed as an "attack" by those who think (wrongly) that I've got it
in for him?

And if he did screw up and I didn't reply, well, so what? I'm under no
obligation to correct /all/ the screw-ups posted here.
really strange
ah: got it ! they're disputating in the good ol' "size of pointers"
thread !

What dispute? The Standard is clear on the matter.
 
R

Richard Tobin

Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
[/QUOTE]
We don't know, and we don't need to care.

Of course we have to care: we have to change any variables that
pointed to the old data so they point to the new data. In effect,
you have to assume that it *is* different.

-- Richard
 
M

mark_bluemel

We don't know, and we don't need to care.

Of course we have to care: we have to change any variables that
pointed to the old data so they point to the new data. In effect,
you have to assume that it *is* different.[/QUOTE]

Fair comment. (This group helps me learn...)
 
D

Default User

I've just checked with a copy of the draft C89 standard and it says
that the return pointer could be either...


Of course. If you think about things in practical fashion (i.e.
implementation details) if it's possible to expand the existing memory
region in place by adjusting a number in a table or something, then it
makes sense to do so. If not, then it pretty much has to be moved.




Brian
 
C

christian.bau

subramanian said:
I have taken the following prototype from K & R.

void *realloc(void *p, size_t size);

Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
K & R 2nd edition says "realloc returns a pointer to the new space".

Why I am asking is that if they are different, the older pointer value
of p should be freed.

realloc can return one of two things: It either returns a null pointer,
or it returns a non-null pointer.

If realloc returns a null pointer, it has failed to reallocate the
memory; the situation is exactly the same as if you had never called
realloc. The old pointer is still valid, and it points to exactly as
much data as it held before.

If realloc returns a non-null pointer, it has reallocated the memory.
The old pointer is now invalid. You need not and you must not do
anything with it. You need not free it and you must not free it. Any
use of it is undefined behavior; even comparing with the new result is
undefined behavior. Here an example; you need to include the right
header files to make it proper C:

void* test_realloc (void* p, size_t new_size)
{
void* q = realloc (p, new_size);
if (q == NULL) {
printf ("realloc failed, p is unchanged.\n");
return p;
} else {
printf ("realloc was successful. Don't even look at p.\n");
return q;
}
}
 
K

Keith Thompson

Default User said:
Of course. If you think about things in practical fashion (i.e.
implementation details) if it's possible to expand the existing memory
region in place by adjusting a number in a table or something, then it
makes sense to do so. If not, then it pretty much has to be moved.

But even a realloc() that shrinks the allocated space or leaves it the
same size *could* relocate it. It might never do so in some
implementations; on the other hand, relocating it might make room for
future allocations.
 
M

mark_bluemel

Default said:
Of course. If you think about things in practical fashion (i.e.
implementation details) if it's possible to expand the existing memory
region in place by adjusting a number in a table or something, then it
makes sense to do so. If not, then it pretty much has to be moved.

I know that, but I feel that particularly in this group it's good to
refer to what the standard says, so you have some grounds for stating
things...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top