int *ptr = malloc(0);
ptr = realloc(ptr, sizeof(int));
>
is this valid?
Yes, it's perfectly legal, whether or not the original malloc returns NULL
- but it is unwise if your code will ever have to work under C/370 on MVS.
Note that, in general, you ought to use a spare pointer when capturing the
result of realloc, just in case it fails. In this case, though, it hardly
matters.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Jul 9, 4:55 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <660a816a-fdd6-491f-9e34-bd3ff9ae2...@k3 7g2000hsf.googl egroups.com>,
Sri Harsha Dandibhotla <harsha....@gma il.comwrote:
>
int *ptr = malloc(0);
ptr = realloc(ptr, sizeof(int));
is this valid?
>
Valid under which standard? C89/C90 or C99?
What happends in each standard with 'malloc(0)'? The wording is not
clear, '0' is not mentioned as a special case.
All ISO C99 says is that it allocates 'size' bytes and returns NULL or
the pointer.
POSIX says 0 is allowed to return a unique pointer or NULL.
What happends in each standard with 'malloc(0)'? The wording is not
clear, '0' is not mentioned as a special case
Yes, it is:
C89: "If the size of the space requested is zero, the behavior is
implementation-defined; the value returned shall be either a null
pointer or a unique pointer."
C99: "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."
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Jul 10, 2:07 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
vipps...@gmail. com said:
>
<snip>
>
What happends in each standard with 'malloc(0)'? The wording is not
clear, '0' is not mentioned as a special case
>
Yes, it is:
>
C89: "If the size of the space requested is zero, the behavior is
implementation-defined; the value returned shall be either a null
pointer or a unique pointer."
>
C99: "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."
Whoops, you are right. That is in 7.20.3 Memory management functions
(n1256.pdf), and I was looking only at 7.20.3.3 "The malloc function"
& 7.20.3.4 "The realloc function".
On Jul 10, 2:07 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>vipps...@gmail .com said:
>>
><snip>
>>
>>What happends in each standard with 'malloc(0)'? The wording is not
>>clear, '0' is not mentioned as a special case
>Yes, it is:
>>
>C89: "If the size of the space requested is zero, the behavior is
>implementati on-defined; the value returned shall be either a null
>pointer or a unique pointer."
>>
>C99: "If the size of the space requested is zero, the behavior is
>implementati on-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."
>
Whoops, you are right. That is in 7.20.3 Memory management functions
(n1256.pdf), and I was looking only at 7.20.3.3 "The malloc function"
& 7.20.3.4 "The realloc function".
You have to read the introduction to the library,
especially
7.1.1 Definitions of terms
7.1.4 Use of library functions
as well as the beginning of the header description
of the function that you're interested in,
to really understand the function descriptions
in the standard.
Most of that, is not difficult reading.
>
>Yes, it's perfectly legal, whether or not the original malloc returns NULL
>- but it is unwise if your code will ever have to work under C/370 on MVS.
What is special about C/370 or MVS (since that is where I spend about
a third of each workday) that makes the code more unwise there than on
a typical desktop system?
>>
>>Yes, it's perfectly legal, whether or not the original malloc returns
>>NULL - but it is unwise if your code will ever have to work under C/370
>>on MVS.
>
What is special about C/370 or MVS (since that is where I spend about
a third of each workday) that makes the code more unwise there than on
a typical desktop system?
On that platform, malloc(0) hangs the process - or at least it used to, a
few years back. (This is not one of the permitted outcomes of malloc(0),
so it counts as an implementation bug.)
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
free(malloc(0)) is not a memory leak (and regardless of what malloc(0)
returns, it's still acceptable to free() ). If you forget to free()
the pointer returned by malloc(n), it's a memory leak regardless
of whether n == 0 or not.
>
Whoops, you are right. That is in 7.20.3 Memory management functions
(n1256.pdf), and I was looking only at 7.20.3.3 "The malloc function"
& 7.20.3.4 "The realloc function".
When consulting the C standard, *always* look at all the parents of the
particular subclause you're interested in; they frequently contain
valuable additional information.
--
Larry Jones
>
Yes, it's perfectly legal, whether or not the original malloc returns NULL
- but it is unwise if your code will ever have to work under C/370 on MVS.
Would you care to expand on that?
--
Larry Jones
ANY idiot can be famous. I figure I'm more the LEGENDARY type! -- Calvin
>>
>Yes, it's perfectly legal, whether or not the original malloc returns
>NULL - but it is unwise if your code will ever have to work under C/370
>on MVS.
>
Would you care to expand on that?
malloc(0) used to cause an abend on that platform. Whether it still does, I
have no idea.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Comment