473,785 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc (0)

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
14 12210

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
-----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 "implementa tion 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

iD8DBQFEqrmCagV FX4UWr64RAt3pAK CDJTOLbg+Dhrtri NiA4PWSbKbJXACf SrtI
Axh2N6HHw/XOOkpbvGYYb2g=
=cE92
-----END PGP SIGNATURE-----

Jul 4 '06 #3
"Kiru Sengal" <ki*********@gm ail.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
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 "implementa tion 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

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

AAAAAAAAaaaaBBB BBBBBCCCCCCCCcc cc
^ ^ ^

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
"Lew Pitcher" <lp******@sympa tico.cawrites:
[...]
The key phrase is "implementa tion 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_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.
Jul 4 '06 #7
Keith Thompson wrote:
"Lew Pitcher" <lp******@sympa tico.cawrites:
[...]
>The key phrase is "implementa tion 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
implementati on 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
Nils O. Selåsdal wrote:
Keith Thompson wrote:
>"Lew Pitcher" <lp******@sympa tico.cawrites:
[...]
>>The key phrase is "implementa tion 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
implementatio n 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
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

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

Similar topics

19
683
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But has it allocated memory or what?
34
6443
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have started messing around in Plan 9, and that sort of thing is totally no go there :). Is this correct to allocate memory for my struct? It works on my computer, but I'm suspicious that I'm doing it wrong. --
231
23247
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
7
2216
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
20
10763
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
2591
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that malloc always returned null at this moment and the program exited (even though if I malloc'ed only 20 bytes or something)... Then I googled for this problem and found something about a memory pool??? Is that standard C? I didn't understand it,...
68
15716
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
40
2604
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd struct? */ Seems to me there is no guarantee it will. /Why Tea
71
19136
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a problem? I cannot see why using malloc instead of new does not give the same result.
23
2731
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef struct pxl { double lon, lat;
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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
10161
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
9958
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...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
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
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
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.