Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: realloc, need to free old ptr?

Reply
Thread Tools

Re: realloc, need to free old ptr?

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      08-26-2003
Tony Finch <(E-Mail Removed)> spoke thus: (on clc.moderated...)

> Note that you should avoid calling realloc() with a size of 0
> since the semantics are ambiguous.


This isn't correct, is it? What does the standard say about it?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
 
Reply With Quote
 
 
 
 
Martin Dickopp
Guest
Posts: n/a
 
      08-26-2003
Christopher Benson-Manica <(E-Mail Removed)> writes:

> Tony Finch <(E-Mail Removed)> spoke thus: (on clc.moderated...)
>
> > Note that you should avoid calling realloc() with a size of 0
> > since the semantics are ambiguous.

>
> This isn't correct, is it? What does the standard say about it?


7.20.3#1: [...] 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.

Martin
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      08-26-2003
Martin Dickopp <(E-Mail Removed)> spoke thus:

> 7.20.3#1: [...] 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.


I see... so is the admonition that the pointer returned should not be used to
access an object directed at the compiler or the programmer? Or both? And
what is the motivation for allowing either of these behaviors?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      08-26-2003

"Christopher Benson-Manica" <(E-Mail Removed)> wrote
[ realloc of 0 ]
> I see... so is the admonition that the pointer returned should not be
> used to access an object directed at the compiler or the programmer?
>

Programmer. A compiler is at liberty to issue a diagnostic if such a pointer
is dereferenced, but it practise it would be too difficult to get a compiler
to do so, because the expression passed to realloc() will probably be
evaluated at run-time.
>
> Or both? And what is the motivation for allowing either of these
> behaviors?
>

It depends how malloc() is implemented. Sometimes it is natural to return a
NULL pointer if no memory is requested, other times the natural thing to do
is to return a dummy block of no memory.
ANSI could have insisted on one behaviour or the other, but they didn't, for
reasons above my level.


 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      09-01-2003
On 26 Aug 2003 19:17:02 +0200, Martin Dickopp
<(E-Mail Removed)> wrote:
<snap>
> > And what is the motivation for allowing either of these behaviors?

>
> This is described in the C rationale
> <http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n897.pdf>.
>

Superseded by n937 in general, although no changes re 7.20.3.

- David.Thompson1 at worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new hard disk on old, old pc Deano Computer Support 5 10-21-2005 12:32 PM
old printer and old WP in Windows XP? lu Computer Support 6 01-14-2005 02:18 PM



Advertisments