473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simple realloc() question

I'm sorry to always bother you guys on simple C topics... but here
again I go :)

What does the standard C99 (and if you have knowledge, pre-C99 also)
say about the behavior of realloc() on different new sizes?

AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails. I was wondering, what happens when you ask realloc to
shrink the allocated space? Will it free the remaining memory or not?

Thanks to anyone!
--
Sensei <senseiwa@Apple's mail>

Research (n.): a discovery already published by a chinese guy one month
before you, copying a russian who did it in the 60s.

Feb 7 '07 #1
11 1599
Sensei wrote:
I'm sorry to always bother you guys on simple C topics... but here
again I go :)

What does the standard C99 (and if you have knowledge, pre-C99 also)
say about the behavior of realloc() on different new sizes?

AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails.
Correct.
I was wondering, what happens when you ask realloc to
shrink the allocated space? Will it free the remaining memory or not?
As far as the standard is concerned, realloc is allowed to return a
null pointer even when asking for less memory than you already have.
It is also allowed to return a non-null pointer that points to more
memory than the amount you asked for.

Feb 7 '07 #2
Sensei wrote, On 07/02/07 08:51:
I'm sorry to always bother you guys on simple C topics... but here again
I go :)

What does the standard C99 (and if you have knowledge, pre-C99 also) say
about the behavior of realloc() on different new sizes?

AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails.
Note that the new address *might* be the same as the old address.
I was wondering, what happens when you ask realloc to
shrink the allocated space? Will it free the remaining memory or not?
It might move on shrinking as well. Whatever happens if the realloc
succeeds (it could fail on shrinking) you can no longer access the
memory you have said you no longer want.
--
Flash Gordon
Feb 7 '07 #3
Sensei wrote:
I'm sorry to always bother you guys on simple C topics... but here
again I go :)

What does the standard C99 (and if you have knowledge, pre-C99 also)
say about the behavior of realloc() on different new sizes?
Nothing that specific.
AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails.
If by "new" you mean "different from the old", then this is not the case.
'realloc' with a bigger size might quite possibly return the same address.
I was wondering, what happens when you ask realloc to
shrink the allocated space? Will it free the remaining memory or not?
If by "free" you me "make available to further allocation requests", then the
answer is "it normally should". But there are many things there that can be done
differently.

--
Best regards,
Andrey Tarasevich
Feb 7 '07 #4
On Feb 7, 10:02 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
Sensei wrote, On 07/02/07 08:51:
I'm sorry to always bother you guys on simple C topics... but here again
I go :)
What does the standard C99 (and if you have knowledge, pre-C99 also) say
about the behavior of realloc() on different new sizes?
AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails.

Note that the new address *might* be the same as the old address.
Not really. The old pointer value is indeterminate if realloc returns
a non-null pointer, so comparing old and new pointer would invoke
undefined behavior.

Feb 7 '07 #5
In article <11**********************@a34g2000cwb.googlegroups .com>,
christian.bau <ch***********@cbau.wanadoo.co.ukwrote:
AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails.
>Note that the new address *might* be the same as the old address.
>Not really. The old pointer value is indeterminate if realloc returns
a non-null pointer, so comparing old and new pointer would invoke
undefined behavior.
If you really want to, you can test whether the address has changed by
copying the old pointer to a suitably-sized byte array before calling
realloc(), and doing the same with the new one. I suppose this could
give false positives if the pointer has padding bits and realloc()
sets them differently.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 7 '07 #6
On Feb 8, 12:09 am, "christian.bau" <christian....@cbau.wanadoo.co.uk>
wrote:
On Feb 7, 10:02 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
Sensei wrote, On 07/02/07 08:51:
I'm sorry to always bother you guys on simple C topics... but here again
I go :)
What does the standard C99 (and if you have knowledge, pre-C99 also) say
about the behavior of realloc() on different new sizes?
AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails.
Note that the new address *might* be the same as the old address.

Not really. The old pointer value is indeterminate if realloc returns
a non-null pointer, so comparing old and new pointer would invoke
undefined behavior.
Yes really. He didn't mention any comparisons, he just stated that the
pointer can have the same value. N1124 says (7.20.3.4): "The realloc
function returns a pointer to the new object (which may have the same
value as a pointer to the old object), or a null pointer if the new
object could not be allocated."
--
WYCIWYG

Feb 8 '07 #7
Richard Tobin wrote:
christian.bau <ch***********@cbau.wanadoo.co.ukwrote:
.... snip stuff that had no corresponding attribution lines ...
>
>Not really. The old pointer value is indeterminate if realloc
returns a non-null pointer, so comparing old and new pointer
would invoke undefined behavior.

If you really want to, you can test whether the address has
changed by copying the old pointer to a suitably-sized byte array
before calling realloc(), and doing the same with the new one. I
suppose this could give false positives if the pointer has
padding bits and realloc() sets them differently.
<academic interest only>
You can beat that by using the %p specifier in a sprintf call.

char inputvalue[ENOUGH];
char outputvalue[ENOUGH];

void *p, *tmp;

p = malloc(SOMETHING];
sprintf(inputvalue, "%p", p);
if (tmp = realloc(p, SOMETHINGELSE)) p = tmp;
else {
puts("No memory"); exit(EXIT_FAILURE);
}
sprintf(outputvalue, "%p, p);
if (strcmp(inputvalue, outputvalue)) puts("pointer changed");
else puts("pointer unchanged after size change");

with suitable #includes and #defines, should be portable.
</academic interest only>

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 8 '07 #8
CBFalconer wrote, On 08/02/07 00:13:
Richard Tobin wrote:
>christian.bau <ch***********@cbau.wanadoo.co.ukwrote:
... snip stuff that had no corresponding attribution lines ...
>>Not really. The old pointer value is indeterminate if realloc
returns a non-null pointer, so comparing old and new pointer
would invoke undefined behavior.
If you really want to, you can test whether the address has
changed by copying the old pointer to a suitably-sized byte array
before calling realloc(), and doing the same with the new one. I
suppose this could give false positives if the pointer has
padding bits and realloc() sets them differently.

<academic interest only>
You can beat that by using the %p specifier in a sprintf call.

char inputvalue[ENOUGH];
char outputvalue[ENOUGH];

void *p, *tmp;

p = malloc(SOMETHING];
sprintf(inputvalue, "%p", p);
if (tmp = realloc(p, SOMETHINGELSE)) p = tmp;
else {
puts("No memory"); exit(EXIT_FAILURE);
}
sprintf(outputvalue, "%p, p);
if (strcmp(inputvalue, outputvalue)) puts("pointer changed");
else puts("pointer unchanged after size change");

with suitable #includes and #defines, should be portable.
</academic interest only>
There is a guarantee that having printed it with %p you c
an get it back with scanf, however I am not aware of any guarantee that
each time you print the same pointer value you will get the same
representation. After all, if it just prints the bytes in hex then if
there are multiple ways of producing the same pointer value the printed
versions would be different.
--
Flash Gordon
Feb 8 '07 #9
"christian.bau" wrote:
>
On Feb 7, 10:02 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
Sensei wrote, On 07/02/07 08:51:
I'm sorry to always bother you guys on simple C topics... but here again
I go :)
What does the standard C99 (and if you have knowledge, pre-C99 also) say
about the behavior of realloc() on different new sizes?
AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails.
Note that the new address *might* be the same as the old address.

Not really. The old pointer value is indeterminate if realloc returns
a non-null pointer, so comparing old and new pointer would invoke
undefined behavior.
Just because you technically can't compare the old and new pointers
doesn't mean that they aren't the same. Besides, he said nothing
about comparing the old and new. He merely mentioned that the new
address might be the same as the old one.
What about this untested code?

==========

#include <stdlib.h>

/* Assumption:
* The output of "%p" formatting must fit in 127 bytes.
*/
int realloc_test(char *oldpt, size_t newsize, char **newpt)
{
char oldbuf[128], newbuf[128];

sprintf(oldbuf,"%p",(void *)oldpt);

*newpt = realloc(oldpt,newsize);
sprintf(newbuf,"%p",(void *)*newpt);

#if DEBUG
printf("%s\n%s\n",oldbuf,newbuf);
#endif
if ( strcmp(oldbuf,newbut) == 0 )
{
return 1;
}
else
{
return 0;
}
}

==========

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Feb 8 '07 #10
In article <5c************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>There is a guarantee that having printed it with %p you c
an get it back with scanf, however I am not aware of any guarantee that
each time you print the same pointer value you will get the same
representation.
Correct -- the scanned pointer only has to compare equal, not
necessarily be the same internal representation.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Feb 8 '07 #11
Kenneth Brody wrote:
"christian.bau" wrote:

On Feb 7, 10:02 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
Sensei wrote, On 07/02/07 08:51:
>
I'm sorry to always bother you guys on simple C topics... but here again
I go :)
>
What does the standard C99 (and if you have knowledge, pre-C99 also) say
about the behavior of realloc() on different new sizes?
>
AFAIR, realloc with a bigger size must return the new address, or NULL
if it fails.
>
Note that the new address *might* be the same as the old address.
Not really. The old pointer value is indeterminate if realloc returns
a non-null pointer, so comparing old and new pointer would invoke
undefined behavior.

Just because you technically can't compare the old and new pointers
doesn't mean that they aren't the same. Besides, he said nothing
about comparing the old and new. He merely mentioned that the new
address might be the same as the old one.
What about this untested code?
[...]
sprintf(oldbuf,"%p",(void *)oldpt);

*newpt = realloc(oldpt,newsize);
sprintf(newbuf,"%p",(void *)*newpt);
[...]
I think it is allowed to give you two identical strings even if the
pointers are really different. You can use memcpy() and memcmp() in a
similar way to avoid that problem; that may tell you the pointers are
different when they really are the same, but if it tells you they are
the same, you can be pretty sure they are.

Feb 8 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Jonas | last post by:
Hi, I'm 99 % sure that Standard C guarantees to do a memory move inside realloc() in case the new, returned memory block (address) is different than the original one. Can any C expert confirm...
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
5
by: James S. Singleton | last post by:
Thanks to everybody who provided an answer to my previous question on this. I am trying to grasp the nitty-gritty details of this, and I am having difficulties understanding some of the issues...
23
by: James Brown | last post by:
Hi all, I just wanted to make sure I understand realloc void *realloc( void *memblock, size_t size ); 1. when memblock is zero, realloc behaves like malloc 2. when memblock is non-zero, and...
27
by: Deephay | last post by:
Greetings all, I have a program that used the realloc() function to change the allocated size of a buffer, the program works with some arguments, but with some other arguments, it will show me...
31
by: banansol | last post by:
Hi, I just want to get this right. A call to realloc() will return NULL on error and the original memory is left untouched, both when requesting a larger or a smaller size that the original,...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.