malloc (0)

B

Bhaskar

Hey can any one tell me what malloc 0 does, it assigns space in memory
i know that but is it usable and how many bytes does it allocate ,
allocation of 0 bytes is difficult-to-digest concept and still in the
memory.
 
K

Kiru Sengal

Bhaskar said:
Hey can any one tell me what malloc 0 does, it assigns space in memory
i know that but is it usable and how many bytes does it allocate ,
allocation of 0 bytes is difficult-to-digest concept and still in the
memory.


I believe it returns NULL.
You're asking for 0 (no) memory to be allocated by doing malloc(0).
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hey can any one tell me what malloc 0 does,

ITYM
malloc(0);

And, the answer is yes and no.
it assigns space in memory i know that

Well, you know wrong, then
but is it usable and how many bytes does it allocate ,

FWIW, you ask for 0 bytes, you might just get zero bytes
allocation of 0 bytes is difficult-to-digest concept and still in the
memory.

The C standards (actually, the draft) says

7.20.3 Memory management functions
[snip]
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 key phrase is "implementation defined". That means that we can't
answer your questions (or even grant that your assertions are correct
or not) without knowing the details of the C implementation you are
using. As I read it, so long as your C compiler/environment's
implementation documents it, malloc(0) could legitimately launch
Russian ICBM missles at your house or cause daemons to fly out of your
nose (the dreaded nasal daemons). Or, it could manipulate its
underlying storage management components so as to remove zero
user-accessable bytes of storage from the appropriate pool of free
storage and place it in a pool of used storage (such an operation would
likely involve an internal allocation of 0+n bytes, where the n
represents the data management overhead).

HTH

- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFEqrmCagVFX4UWr64RAt3pAKCDJTOLbg+DhrtriNiA4PWSbKbJXACfSrtI
Axh2N6HHw/XOOkpbvGYYb2g=
=cE92
-----END PGP SIGNATURE-----
 
A

Antoine Junod

Kiru Sengal said:
I believe it returns NULL.

I don't think so:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv){
int *i = (int *)malloc(0);

printf("%p\n", (void *)i);
printf("%p\n", NULL);
return 0;
}

output me:
toto@epia ~ $ ./a.out
0x804a050
(nil)
 
G

Guest

Lew said:
Bhaskar said:
Hey can any one tell me what malloc 0 does,

ITYM
malloc(0);

And, the answer is yes and no.
it assigns space in memory i know that

Well, you know wrong, then
but is it usable and how many bytes does it allocate ,

FWIW, you ask for 0 bytes, you might just get zero bytes
allocation of 0 bytes is difficult-to-digest concept and still in the
memory.

The C standards (actually, the draft) says

7.20.3 Memory management functions
[snip]
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 key phrase is "implementation defined". That means that we can't
answer your questions (or even grant that your assertions are correct
or not) without knowing the details of the C implementation you are
using. As I read it, so long as your C compiler/environment's
implementation documents it, malloc(0) could legitimately launch
Russian ICBM missles at your house or cause daemons to fly out of your
nose (the dreaded nasal daemons).

"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."

Unless either returning a null pointer by itself in a user function, or
calling malloc with a nonzero size, is allowed to launch missiles, no,
malloc(0) isn't allowed to launch missiles either. Either it just
returns a null pointer, or it behaves (almost) as, for example,
malloc(1).
 
M

Malcolm

Bhaskar said:
Hey can any one tell me what malloc 0 does, it assigns space in memory
i know that but is it usable and how many bytes does it allocate ,
allocation of 0 bytes is difficult-to-digest concept and still in the
memory.
It can either return a pointer to a block of no length, or it can return a
null pointer.

A block of no length might seem a slightly Hindu concept, but it is not that
difficult for anyone to understand, even if they don't apprecuiate all the
subtleties of zero..
Imagine a control block of 8 bytes which gieves information about the memory
allocated, so that free() can return it properly to the pool. malloc returns
a pointer to the meory immediately after this block.

Now image we allocate block A, of 4 bytes, and block B od zero bytes, the
block C of 4 bytes.

AAAAAAAAaaaaBBBBBBBBCCCCCCCCcccc
^ ^ ^

The Chinese hats represent the pointers returned. When we call free(),
internally 8 bytes are subtracted and this tells the system where the
control information is.
 
K

Keith Thompson

Lew Pitcher said:
The key phrase is "implementation defined". That means that we can't
answer your questions (or even grant that your assertions are correct
or not) without knowing the details of the C implementation you are
using.
Correct.

As I read it, so long as your C compiler/environment's
implementation documents it, malloc(0) could legitimately launch
Russian ICBM missles at your house or cause daemons to fly out of your
nose (the dreaded nasal daemons).
[snip]

No. It's implementation-defined behavior, not undefined behavior.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Keith said:
Lew Pitcher said:
The key phrase is "implementation defined". That means that we can't
answer your questions (or even grant that your assertions are correct
or not) without knowing the details of the C implementation you are
using.
Correct.

As I read it, so long as your C compiler/environment's
implementation documents it, malloc(0) could legitimately launch
Russian ICBM missles at your house or cause daemons to fly out of your
nose (the dreaded nasal daemons).
[snip]

No. It's implementation-defined behavior, not undefined behavior.

And in either case, the return value of malloc(0) is safe to pass
to free.
 
C

Clever Monkey

Nils said:
Keith said:
Lew Pitcher said:
The key phrase is "implementation defined". That means that we can't
answer your questions (or even grant that your assertions are correct
or not) without knowing the details of the C implementation you are
using.
Correct.

As I read it, so long as your C compiler/environment's
implementation documents it, malloc(0) could legitimately launch
Russian ICBM missles at your house or cause daemons to fly out of your
nose (the dreaded nasal daemons).
[snip]

No. It's implementation-defined behavior, not undefined behavior.

And in either case, the return value of malloc(0) is safe to pass
to free.
True, but this is really about the standard behaviour for free(), rather
than anything specific to malloc() or its return. That is, the standard
says that when passing a null pointer to free() it does nothing.
 
J

Joe Wright

Bhaskar said:
Hey can any one tell me what malloc 0 does, it assigns space in memory
i know that but is it usable and how many bytes does it allocate ,
allocation of 0 bytes is difficult-to-digest concept and still in the
memory.
Try this.

#include <stdio.h>
#include <stdlib.h>
int main(void) {
void *vp = malloc(0);
printf("malloc(0) returned %p\n", vp);
return 0;
}

Here at home,

malloc(0) returned 902a0

vp is non-null and so I assume that malloc did what I asked and
allocated 0 bytes successfully. Do you think I should free(vp) to avoid
a leak?
 
E

Eric Sosman

Joe said:
Try this.

#include <stdio.h>
#include <stdlib.h>
int main(void) {
void *vp = malloc(0);
printf("malloc(0) returned %p\n", vp);
return 0;
}

Here at home,

malloc(0) returned 902a0

vp is non-null and so I assume that malloc did what I asked and
allocated 0 bytes successfully. Do you think I should free(vp) to avoid
a leak?

Not in this program, but in general yes! Otherwise, you
may well leak some extra "bookkeeping" memory that malloc()
uses to keep track of each zero-length allocation. Try

#include <stdio.h>
#include <stdlib.h>
int main(void) {
unsigned long count = 0;
while (malloc(0) != NULL) {
if (++count == 0) {
printf ("More than %lu successful allocations"
" -- giving up.\n", (unsigned long)-1);
return 0;
}
}
printf ("%lu successful allocations before NULL\n",
count);
return 0;
}

"Here at home," I get

72960776 successful allocations before NULL

.... plus a lot of disk activity and some system alerts about
increasing the size of the page file. Strongly suggestive of
leakage, I'd say.
 
J

Joe Wright

Eric said:
Not in this program, but in general yes! Otherwise, you
may well leak some extra "bookkeeping" memory that malloc()
uses to keep track of each zero-length allocation. Try

#include <stdio.h>
#include <stdlib.h>
int main(void) {
unsigned long count = 0;
while (malloc(0) != NULL) {
if (++count == 0) {
printf ("More than %lu successful allocations"
" -- giving up.\n", (unsigned long)-1);
return 0;
}
}
printf ("%lu successful allocations before NULL\n",
count);
return 0;
}

"Here at home," I get

72960776 successful allocations before NULL

... plus a lot of disk activity and some system alerts about
increasing the size of the page file. Strongly suggestive of
leakage, I'd say.
Leaks like a sieve. 73 million useless calls to malloc(0) and you
couldn't free() them if you wanted to. Crash and Burn.
 
M

Mark McIntyre

Lew Pitcher wrote:
Unless either returning a null pointer by itself in a user function, or
calling malloc with a nonzero size, is allowed to launch missiles, no,
malloc(0) isn't allowed to launch missiles either.

Actually it is, but the implementation must define this behaviour. The
DS9K's C compiler provides a very similar feature.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
S

Skarmander

Mark said:
Actually it is, but the implementation must define this behaviour. The
DS9K's C compiler provides a very similar feature.

Although it hardly matters in this less than serious context, the DS9K
compiler would not be conforming for doing so. (Which would be odd to say
the least, since the DS9K manufacturer is usually very careful about this
sort of thing.)

N1124:
"3.4.1 implementation-defined behavior
unspecified behavior where each implementation documents how the choice is made"

"3.4.4 unspecified behavior
use of an unspecified value, or other behavior where this International
Standard provides two or more possibilities and imposes no further
requirements on which is chosen in any instance"

Even the most liberal interpretation of these statements requires that, if
the standard gives a choice between behaviors and states the actual behavior
is "implementation-defined", then the choice must be made from the behaviors
given and the choice itself must be documented. The implementation is not
allowed to implement additional or replacement behavior, not even if it
documents it.

The alternative would make "unspecified behavior" a synonym for "undefined
behavior", which is clearly not the intent.

S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top