By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

malloc (0)

P: n/a
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.

Jul 4 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a

Bhaskar wrote:
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).

Jul 4 '06 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Bhaskar wrote:
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+DhrtriNiA4P WSbKbJXACfSrtI
Axh2N6HHw/XOOkpbvGYYb2g=
=cE92
-----END PGP SIGNATURE-----

Jul 4 '06 #3

P: n/a
"Kiru Sengal" <ki*********@gmail.comwrites:
Bhaskar wrote:
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.
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)
Jul 4 '06 #4

P: n/a
Lew Pitcher wrote:
Bhaskar wrote:
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).

Jul 4 '06 #5

P: n/a

"Bhaskar" <bh***********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
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.


--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jul 4 '06 #6

P: n/a
"Lew Pitcher" <lp******@sympatico.cawrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) 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.
Jul 4 '06 #7

P: n/a
Keith Thompson wrote:
"Lew Pitcher" <lp******@sympatico.cawrites:
[...]
>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.

Jul 4 '06 #8

P: n/a
Nils O. Selåsdal wrote:
Keith Thompson wrote:
>"Lew Pitcher" <lp******@sympatico.cawrites:
[...]
>>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.
Jul 4 '06 #9

P: n/a
Bhaskar wrote:
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?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 4 '06 #10

P: n/a
Joe Wright wrote:
Bhaskar wrote:
>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?
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.

--
Eric Sosman
es*****@acm-dot-invalid.org
Jul 4 '06 #11

P: n/a
Eric Sosman wrote:
Joe Wright wrote:
>Bhaskar wrote:
>>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?

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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 4 '06 #12

P: n/a
On 4 Jul 2006 12:20:41 -0700, in comp.lang.c , "Harald van D?k"
<tr*****@gmail.comwrote:
>Lew Pitcher wrote:
>The key phrase is "implementation defined".
....
>>As I read it, so long as your C compiler/environment's
implementation documents it, malloc(0) could legitimately launch
Russian ICBM missles
>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
Jul 4 '06 #13

P: n/a
Mark McIntyre wrote:
On 4 Jul 2006 12:20:41 -0700, in comp.lang.c , "Harald van D?k"
<tr*****@gmail.comwrote:
>Lew Pitcher wrote:
>>The key phrase is "implementation defined".
...
>>As I read it, so long as your C compiler/environment's
implementation documents it, malloc(0) could legitimately launch
Russian ICBM missles
>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.
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.
Jul 4 '06 #14

P: n/a
"Kiru Sengal" <ki*********@gmail.comwrites:
Bhaskar wrote:
>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.
This is one possible implementation.
It may also return a unique address that may not be dereferenced.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Jul 5 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.