473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

realloc() implicit free() ?

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 that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.

I had always assumed it would automatically free the previous memory,
but is the behaviour instead undefined [or defined as not happening] ?
--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD
Nov 14 '05
86 4175
In article <11************ *@corp.supernew s.com>,
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
ro******@ibd.n rc-cnrc.gc.ca (Walter Roberson) wrote: # My user and I would be happy to live with whatever the interface
# specification -is-, but what *is* that specification?? Where is it
# written in the C89 standard what exactly will happen? The specification is the input pointer value is generally no longer valid
and should not be used again in any calls or memory references;


Could you be so kind as to indicate where in C89 that specification
is given?

I've been reading and re-reading 4.10.3 "Memory Management
Functions" and I cannot seem to find any wording to that effect.

I'm not referring to "common practices" or "what a good implementation
would do". Any interface promise not written down in a C standard
is subject to alteration in the next edition of the DS9000 compiler.

But more seriously, any interface promise not written down in
a C standard is subject to alteration in the next edition of gcc
(and subject to being changed back in the dot release after that,
and changed back again in a later dot release, until eventually
RS decides that it's important to keep the odd behaviour in order
to prevent someone from putting out a software patent on it...)
--
Any sufficiently old bug becomes a feature.
Nov 14 '05 #61
ra***********@g mail.com wrote:

[ much snippage ]

As I was going through the Recent replies on the realloc(),
I got some question and my annalysis on that, so regarding on these
please guide me where I fail on the theoritical and practical
Knowledge. I am not able to read all the thread in the replies as
due to some problem in the web server.

Point 1.

If we do the realloc then it means that we have allocated the
extended memory for the current memory, for which we have
reallocated it. Means I need not to free the previous memory
which I extendend to realloc if compiler allocates memory
(extended memory) from the place where intial memory was allocated.
Yes. You only free() the last address returned by realloc().
And we need to free if the memory is allocated by the
(realloc)in the new region.
No. There are no regions in play here.
so the key is to always free the memory when you reallocate
the memory by realloc fucntion.
No. You only free() the last address returned by realloc().

How much I am correct on the Point 1 ?

Point 2.

what is the diffrence between the calloc() and malloc()
As far As I know the basic diffrence is that
1. malloc takes 1 argumnets while calloc takes two Yes. 2. malloc initialise the memory with the garbage values while
calloc initialise it with 0 (Zero) malloc() does not initialize memory. 3. malloc allocates continious memeory i.e one Block while
calloc alloactes into the Block
calloc (100, 2) ,means two block of 100 memoty alloaction. No. malloc() and calloc() both allocate contiguous memory. The two
arguments to calloc() have no particular signifigance. (100,2) allocates
200 bytes.
apart from the above is any more diffrence between them ??

Point 3.

This may be looks off topic to you but I have one thing to ask
is there any diffrence between the malloc and new ??
Yes. malloc() is defined by C, new is not.
Point 4.

why we need to derefrence the pointers once we are are done with our
work; I am not aware of garbage collection, And where I can find the
memory leak into the program ?
Wrong term. In C we dereference a pointer to yield the value to which it
points. free() does not do that. Rather free() de-allocates memory
allocated by its *alloc() siblings.
Thanks In Advance
Regards
Ranjeet

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #62
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:
# In article <11************ *@corp.supernew s.com>,
# SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
# >ro******@ibd.n rc-cnrc.gc.ca (Walter Roberson) wrote:
#
# ># My user and I would be happy to live with whatever the interface
# ># specification -is-, but what *is* that specification?? Where is it
# ># written in the C89 standard what exactly will happen?
#
# >The specification is the input pointer value is generally no longer valid
# >and should not be used again in any calls or memory references;
#
# Could you be so kind as to indicate where in C89 that specification
# is given?

page 155 line 19-20
page 156 line 22-24
page 144 line 14-16
# I've been reading and re-reading 4.10.3 "Memory Management
# Functions" and I cannot seem to find any wording to that effect.

If you wanted a verbatim transcript of the copyrighted work, why not ask
for it. Or if you have a copy and can't understand it, why do you need
someone to repeat words you are apparently incapable of understanding?

Or are you simply interesting in creating an argument because
you're bored?

# I'm not referring to "common practices" or "what a good implementation
# would do". Any interface promise not written down in a C standard
# is subject to alteration in the next edition of the DS9000 compiler.

You've got bigger problems than what the DS9000 compiler is doing.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
This is one wacky game show.
Nov 14 '05 #63
[ Obnoxious '#' quoting character fixed. ]
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> writes:
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:
In article <11************ *@corp.supernew s.com>,
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
ro******@ibd.n rc-cnrc.gc.ca (Walter Roberson) wrote:

> My user and I would be happy to live with whatever the interface
> specification -is-, but what *is* that specification?? Where is it
> written in the C89 standard what exactly will happen?

The specification is the input pointer value is generally no longer valid
and should not be used again in any calls or memory references;


Could you be so kind as to indicate where in C89 that specification
is given?


page 155 line 19-20
page 156 line 22-24
page 144 line 14-16


My copy of the C90 standard (ANSI/ISO 9899-1900) does not have line
numbers on each page. I own both a hard copy (the left-hand pages of
Schildt's annotated version) and a soft copy, in PDF format. I do not
have a copy of the actual 1989 ANSI standard; my understanding is that
they are essentially identical except for some introductory matter and
some changes in the section numbering. Apparently the page numbers
also changed. Page 155 of the C90 standard does contain the
description of the realloc function, but page 156 describes atexit,
exit, and getenv, and page 144 describes ungetc and fread.

It's more useful to provide section numbers rather than page numbers.
For example, C90 7.10.3.4 describes the realloc function; that's
probably 4.10.3.4 in the C89 ANSI standard. It's even more useful to
provide brief quotations (I believe that's covered under fair use, but
IANAL).

Earlier today, I posted a followup in this thread, message-id
<ln************ @nuthaus.mib.or g>, in which I directly quoted the
section of the C90 standard that describes realloc(). Perhaps you can
reply to that and point out what you think I'm missing. It's
available at
<http://groups-beta.google.com/group/comp.lang.c/msg/988166ae47c087c b>.
I've been reading and re-reading 4.10.3 "Memory Management
Functions" and I cannot seem to find any wording to that effect.


If you wanted a verbatim transcript of the copyrighted work, why not ask
for it. Or if you have a copy and can't understand it, why do you need
someone to repeat words you are apparently incapable of understanding?

Or are you simply interesting in creating an argument because
you're bored?


I have also read the corresponding section of the C90 standard, and I
cannot find any explicit wording about the circumstances in which
realloc deallocates memory. We all know that that's the intent, but I
can't find the wording that expresses that intent.

In my opinion, the description of realloc in the C89/C90 standard was
poorly worded, and did not actually state that the old block of memory
is deallocated if a new one is allocated. This flaw is corrected in
the C99 standard, and commented on in the C99 Rationale.

It's entirely possible that I've missed something in the C90 standard
that makes this point clear. If so, I invite you to point it out to
us. Please note that insults about my reading comprehension skills do
not constitute pointing it out.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #64
Keith Thompson <ks***@mib.or g> wrote:
My copy of the C90 standard (ANSI/ISO 9899-1900) does not have line
numbers on each page. I own both a hard copy (the left-hand pages of
Schildt's annotated version) and a soft copy, in PDF format.


Where did you get C90 in PDF format, and how much does it cost?

--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #65
"S.Tobias" <si***@FamOuS.B edBuG.pAlS.INVA LID> writes:
Keith Thompson <ks***@mib.or g> wrote:
My copy of the C90 standard (ANSI/ISO 9899-1900) does not have line
numbers on each page. I own both a hard copy (the left-hand pages of
Schildt's annotated version) and a soft copy, in PDF format.


Where did you get C90 in PDF format, and how much does it cost?


I bought it from ANSI for $18. I don't know whether it's still
available.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #66
Keith Thompson <ks***@mib.or g> wrote:
"S.Tobias" <si***@FamOuS.B edBuG.pAlS.INVA LID> writes:
Keith Thompson <ks***@mib.or g> wrote: Where did you get C90 in PDF format, and how much does it cost?
I bought it from ANSI for $18. I don't know whether it's still
available.


They sell only C99 now.

--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #67
CBFalconer wrote:
If the size of the space requested is
zero, the behavior is implementation-defined: either a null

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
pointer is returned, or the behavior is as if the size were
some nonzero value, except that the returned pointer shall
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^
not be used to access an object. The value of a pointer
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^
that refers to freed space is indeterminate.
The keyword is 'implementation defined'. It's up to the code to decide
what to do with a malloc(0) request : return NULL, return a unusable
pointer, or return a pointer with a certain usable space (1 byte is
commonly used).

Since 0 bytes isn't usable at all, you can simply assume that malloc(0)
doens't result in anything usable. My malloc() wrapper simply asserts on
a 0 byte request.
Is freeing NULL so generated accessing an object? Is freeing
non-NULL so generated accessing an object? When things are buried
in layers of code we can easily guarantee that the pointer was
created by malloc/realloc, and we can even guard it by making it
NULL after passing to free, but that guard won't work if realloc
can return a non-freeable pointer.


It doesn't say non-freeable : It says 'not be used to access an object'.
In other words : You can't use the allocated space.


Igmar
Nov 14 '05 #68
In article <11************ *@corp.supernew s.com>,
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
ro******@ibd.n rc-cnrc.gc.ca (Walter Roberson) wrote:
# In article <11************ *@corp.supernew s.com>,
# SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
# >ro******@ibd.n rc-cnrc.gc.ca (Walter Roberson) wrote: # ># My user and I would be happy to live with whatever the interface
# ># specification -is-, but what *is* that specification?? Where is it
# ># written in the C89 standard what exactly will happen? # >The specification is the input pointer value is generally no longer valid
# >and should not be used again in any calls or memory references; # Could you be so kind as to indicate where in C89 that specification
# is given? page 155 line 19-20
19 zero, the behaviour is implementation-defined; the value returned
shall be either a null pointer or a
20 unique pointer. The value of a pointer that refers to freed space
is indeterminate.
page 156 line 22-24
22 the callc, malloc, or reallc function, or if the space has been
deallocated by a call to
23 the free or realloc function, the behavior is undefined. If the
space cannot be allocated, the
24 object pointed to by ptr is unchanged. If size is zero and ptr
is not a null pointer, the object

page 144 line 14-16
14 int putc(int c, FILE *stream) ;

15 Description

16 The putc function is equivalent to fputc, except that if it
is implemented as a macro, it

# I've been reading and re-reading 4.10.3 "Memory Management
# Functions" and I cannot seem to find any wording to that effect. If you wanted a verbatim transcript of the copyrighted work, why not ask
for it.
I have the hard copy. I can't find any date when the copy itself was
printed, but it is old enough to literally be falling apart with use,
especially at page 154/155.
Or if you have a copy and can't understand it, why do you need
someone to repeat words you are apparently incapable of understanding?
Well, the C89 standard is a large document, and sometimes important
points in it are not in the obvious places, so I was hoping you had
found a section that I had missed. The sections you indicated above
unfortunately do not establish the validity of the claims you have
made.

If you re-examine page 156 lines 17 through 25, you will find
that deallocation is mentioned in two contexts:

a) that if the space pointed to has already been deallocated by
free() or realloc(), the behaviour is undefined;

b) that if the size is 0 and the pointer is non-null, the object
is freed.

Thus, the *only* instance in section 4.10.3.4 (page 156) in which
realloc() promises to free memory is if the size is 0 (and the
pointer non-null). There is no interface promise in *that* section
that the old object will be freed when the data is moved.
And there isn't any such interface promise about realloc() in
section 4.10.3 "Memory Management Functions" (page 155) either.

Thus, the wording in 4.10.3.4 about indeterminate behaviour
when "space has been deallocated by a call to the free() or
realloc() function" [pg 156 lines 22-23] is only certain to apply
if when realloc()'d with a size of 0. Otherwise, if one changed
sizes and realloc() happened to move the memory, then the
old pointer *does* "match a pointer earlier returned by
the calloc(), malloc(), or realloc() function" [pg 156 lines 21-22],
and in that instance, the C89 standard does not express any
indeterminacy of the operation.

Or are you simply interesting in creating an argument because
you're bored?
Not at all. You seem very certain that certain "modular
programming" interface promises have been made in the C89
definition of realloc(), but when I [or anyone else I've shown
the section to] examine the standards, we don't seem to find those
interface promises anywhere. Many would assume that you are wrong,
but I assume that you have a good reason for saying what
you are saying, and I ask for clarification.

You've got bigger problems than what the DS9000 compiler is doing.


Sure do. I'm in the middle of trying to convince my management to
spend a raft of money on network improvements. Interpreting the C89
standard is simple compared to convincing scientists to contribute
a share of their grants to a common cause.
--
Look out, there are llamas!
Nov 14 '05 #69
In article <d6**********@c anopus.cc.umani toba.ca>,
Walter Roberson <ro******@ibd.n rc-cnrc.gc.ca> wrote:
my question is: in the case that realloc() returns a different
pointer than was input, then to avoid memory leaks, is it necessary
or forbidden to free() the memory at the previous pointer -- or is it
not specified ?
Everybody else seems to understand that; there's no need to waste your
time and energy arguing it with this guy. (Unless, of course, you enjoy
doing so. Everybody who's not actively participating has either made
popcorn or killfiled the thread by now.)

The other posters have pointed to language in C99
that make it clear for C99, but I (the OP) and a number of other posters
have not been able to find any "promise" or interface specification
in C89 that says unambiguously what the behaviour is. (And that's
not even counting the sub-discussion about realloc() with size 0.)

My user and I would be happy to live with whatever the interface
specificatio n -is-, but what *is* that specification?? Where is it
written in the C89 standard what exactly will happen?


The draft I have describes realloc's behavior as "changes the size of the
object pointed to by ptr" (4.10.3.4; I believe this is the ANSI numbering
but I'm not certain - it's the draft that came from Dan Pop's collection).
If it's changing the size and not creating a new one, there's no old
one left behind for you to free.

So it seems to me that the specification is there, it's just not as
clear as in C99.
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
Hmmm, I know some folks are not at all disturbed by getting flamed,
but it's not often I see someone actively try to jump into a fire.
--Mike Wahler in comp.lang.c
Nov 14 '05 #70

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

Similar topics

27
31465
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 the error message like: *** glibc detected *** realloc(): invalid next size: 0x0804c3a8 *** and then I inserted a perror("realloc") to see what happend, it says that:
0
9683
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10457
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10013
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6792
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4119
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
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.