473,569 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does standard C guarantee same memory content after realloc()?

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 this to me, please?

Thanks,
Jonas

PS. I using C90, not C99--if it makes a difference.
Nov 13 '05 #1
20 3041
In article <rJ************ *******@newsb.t elia.net>, Jonas wrote:
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 this to me, please?


I haven't got the C90 standard in my hand, but SUSv3 (which is a
ISO C superset) says about realloc() that "The contents of the
object shall remain unchanged up to the lesser of the new and
old sizes".

--
Andreas Kähäri
Nov 13 '05 #2
> 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 this to me, please?


The memory location will only change if at the current position there is no
room for a continous block with the requested size.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #3
Andreas Kahari <ak*******@free shell.org> wrote:
In article <rJ************ *******@newsb.t elia.net>, Jonas wrote:
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 this to me, please?


I haven't got the C90 standard in my hand, but SUSv3 (which is a
ISO C superset) says about realloc() that "The contents of the
object shall remain unchanged up to the lesser of the new and
old sizes".


ISO/IEC 9899:1999 states:

7.20.3.4 The realloc function
[...]
2 The realloc function deallocates the old object pointed to by ptr
and returns a pointer to a new object that has the size specified
by size. The contents of the new object shall be the same as that
of the old object prior to deallocation, up to the lesser of the
new and old sizes. Any bytes in the new object beyond the size of
the old object have indeterminate values.
A draft copy of ANSI C89 I found reads:

4.10.3.4 The realloc function
[...]
The realloc function changes the size of the object pointed to by
ptr to the size specified by size . The contents of the object shall
be unchanged up to the lesser of the new and old sizes. If the new
size is larger, the value of the newly allocated portion of the object
is indeterminate.

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #4
"cody" <do************ *********@gmx.d e> wrote:
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 this to me, please?


The memory location will only change if at the current position there is no
room for a continous block with the requested size.


Maybe true. Maybe not. (At least) C99 does not require this behaviour,
though many implementations will behave like this. However, how does
your statement address the OPs question?

To OP: see my other reply.

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #5
In <bm************ @ID-176797.news.uni-berlin.de> "cody" <do************ *********@gmx.d e> writes:
I'm 99 % sure that Standard C guarantees to do a memory move inside
realloc() in case the new, returned memory block (address) is differentthan
the original one. Can any C expert confirm this to me, please?


You can be safely 100% sure of that. Under both C standards.
The memory location will only change if at the current position there is no
room for a continous block with the requested size.


1. This statement is false. realloc() is free to change the block's
address even if the old size is equal to the new size. Under NO
circumstance is realloc() constrained to return the old address.

2. This has absolutely NOTHING to do with the OP's question! He was
talking about the *contents* of the reallocated memory block, not
about the circumstances when the memory block will have its address
changed.

So, could you, please, stop posting bullshit to this newsgroup?

The actual C89 specification is:

4.10.3.4 The realloc function

Synopsis

#include <stdlib.h>
void *realloc(void *ptr, size_t size);

Description

The realloc function changes the size of the object pointed to by
ptr to the size specified by size . The contents of the object shall
be unchanged up to the lesser of the new and old sizes. If the new
size is larger, the value of the newly allocated portion of the object
is indeterminate. If ptr is a null pointer, the realloc function
behaves like the malloc function for the specified size. Otherwise,
if ptr does not match a pointer earlier returned by the calloc ,
malloc , or realloc function, or if the space has been deallocated by
a call to the free or realloc function, the behavior is undefined. If
the space cannot be allocated, the object pointed to by ptr is
unchanged. If size is zero and ptr is not a null pointer, the object
it points to is freed.

Returns

The realloc function returns either a null pointer or a pointer to
the possibly moved allocated space.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
> >> 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 this to me, please?
You can be safely 100% sure of that. Under both C standards.
The memory location will only change if at the current position there is

noroom for a continous block with the requested size.


1. This statement is false. realloc() is free to change the block's
address even if the old size is equal to the new size. Under NO
circumstance is realloc() constrained to return the old address.

2. This has absolutely NOTHING to do with the OP's question! He was
talking about the *contents* of the reallocated memory block, not
about the circumstances when the memory block will have its address
changed.

So, could you, please, stop posting bullshit to this newsgroup?

Instead of giving stupid and rude comments you should read the original
question.
The OP clearly asked wheather the returned memory address will/might/can be
different than the original one.

You are talking bullshit here. Why in the world should realloc cause the
content of the memory block to be changed???

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #7
PLEASE do not drop references when quoting!

[quoting restored]

"cody" <do************ *********@gmx.d e> wrote:
Dan Pop wrote:
cody wrote:
> Jonas wrote:
>> I'm 99 % sure that Standard C guarantees to do a memory move inside
>> realloc() in case the new, returned memory block (address) is differentthan >> the original one. Can any C expert confirm this to me, please?
You can be safely 100% sure of that. Under both C standards.
>The memory location will only change if at the current position there isno >room for a continous block with the requested size.


1. This statement is false. realloc() is free to change the block's
address even if the old size is equal to the new size. Under NO
circumstance is realloc() constrained to return the old address.

2. This has absolutely NOTHING to do with the OP's question! He was
talking about the *contents* of the reallocated memory block, not
about the circumstances when the memory block will have its address
changed.

So, could you, please, stop posting bullshit to this newsgroup?


Instead of giving stupid and rude comments you should read the original
question.


I never encountered a stupid comment given by Dan Pop. If you dislike
his, um, direct way of commenting, or consider it rude, feel free to
ignore his posts. But be aware you definitely miss something when doing
so.
The OP clearly asked wheather the returned memory address will/might/can be
different than the original one.
Oh yes? Let's review the subject line of this thread:

"Does standard C guarantee same memory content after realloc()?"

Now, which part of "same memory content" didn't you understand?
You are talking bullshit here.
Hmwah. Hmmwaha. Hmwahaaaha-ha-haaa. Well, I know for sure that /if/
somebody is posting bovine excrement here at all, it is you.
Why in the world should realloc cause the
content of the memory block to be changed???


I do not know, but the question was: is it required by the standard
for realloc to retain the contents of the memory (except that truncation
occurs when the new size is less than the previous size)?
[ Answer: definetely yes, in C89/C90 and C99 ]

Now stop trolling and go read a book on C.

Irrwahn
--
ERROR 103: Dead mouse in hard drive.
Nov 13 '05 #8
cody wrote:
Instead of giving stupid and rude comments you should read the original
question.
Stop spewing a bunch of incorrect information then.
The OP clearly asked wheather the returned memory address will/might/can be
different than the original one.


I think you better reread.

"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 this to me, please?"
The OP already acknowledges that the address may change, he wanted to
know if the data went with it.


Brian Rodenborn
Nov 13 '05 #9
On Fri, 10 Oct 2003 21:35:31 +0200
"cody" <do************ *********@gmx.d e> wrote:

Please don't trim out the attribution lines for bits of the message
still quoted.
> 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 this to me, please?
You can be safely 100% sure of that. Under both C standards.
The memory location will only change if at the current position
there is noroom for a continous block with the requested size.


1. This statement is false. realloc() is free to change the block's
address even if the old size is equal to the new size. Under NO
circumstance is realloc() constrained to return the old address.

2. This has absolutely NOTHING to do with the OP's question! He was
talking about the *contents* of the reallocated memory block, not
about the circumstances when the memory block will have its
address changed.

So, could you, please, stop posting bullshit to this newsgroup?

Instead of giving stupid and rude comments you should read the
original question.


Dan did both read and answer the OPs question.
The OP clearly asked wheather the returned memory address
will/might/can be different than the original one.
No, the OP asked if it does a memory move *when* the return address is
different. I.e. whether he runs the risk of loosing his data.
You are talking bullshit here. Why in the world should realloc cause
the content of the memory block to be changed???


It is allowed to therefor one should allow for the fact it could happen.
As to why it might happen in a real system, it could do it to
consolidate free space.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #10

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

Similar topics

37
4620
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the values in (as would be required if I used the hideous, evil, much-abused #define :) ----------- const int constantA = 10; static const int...
24
3776
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
8
1681
by: Francois Grieu | last post by:
I guess this program has undefined behavior, as far as ISO C is concerned. Anyone knows an implementation where realloc actually moves a block that it shortens (as opposed to grows?) #include <stdlib.h> #include <string.h> #include <stdio.h>
1
1825
by: David Resnick | last post by:
I had a problem going from gcc 2.96 to gcc 3.2.3 and narrowed it down to the following code. The question is whether the problem is undefined behavior on the code's part or a compiler bug. #include <stdlib.h> #include <stdio.h> struct frob { char a; int b;
31
3686
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the malloc package to find out how big an allocated block is". ( Question 7.27) Is there somwhere explained why - because it would seem to me, that free()
7
2915
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
7
1459
by: grocery_stocker | last post by:
Given the following snippet of code: (taken from http://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html) void * xmalloc (size_t size) { register void *value = malloc (size); if (value == 0) fatal ("virtual memory exhausted");
89
5968
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
270
9310
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting to see that the answers to that message prove that programming exclusively in standard C is completely impossible even for a small and ridiculously...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.