473,881 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc(0)

Hi all,

What is the significance of "malloc(0)" ?
It doesn't return a NULL pointer, then what does it return?
Where one can use such a "malloced" pointer?

Thanks for help.

Regards,
Shailendra
Nov 14 '05 #1
13 2948
"Mehta Shailendrakumar " <sh************ *******@de.bosc h.com> wrote:
What is the significance of "malloc(0)" ?
Nearly nothing.
It doesn't return a NULL pointer, then what does it return?
In C89, "a unique pointer"; in C99, "as if the size were some nonzero
value, except that the returned pointer shall not be used to access an
object." In neither case is the pointer much use; you can't dereference
it, and its value is as good as random.
Where one can use such a "malloced" pointer?


Nowhere. Well, one can pass it to free() without worrying that this will
cause problems.
The only way in which this is useful is when you're not talking about
malloc(0) itself, but about realloc(0). That is a possible marginal case
in automatic allocation functions (e.g., for string handling), and in
such situations it's useful that it doesn't cause undefined behaviour.
That, AFAICT, is all.

Richard
Nov 14 '05 #2
On Mon, 20 Jun 2005 10:39:09 +0200, Mehta Shailendrakumar wrote:
Hi all,

What is the significance of "malloc(0)" ?
Nothing great, malloc() happens to allow 0 as a valid argument.
It doesn't return a NULL pointer, then what does it return?
It can return a null pointer, any call to malloc() can return a null
pointer. However some implementations always return null for malloc(0)
where they don't for other sizes.
Where one can use such a "malloced" pointer?


It can be passed to realloc() and free(). It can also be compared against
other pointers where it must compare unequal to other pointer to object
values and null. Unfortunately since you aren't guaranteed to get a
non-null return for malloc(0) even under "normal" conditions where you
would expect malloc() to return non-null this isn't a portable application.

Lawrence
Nov 14 '05 #3

On 20/06/2005 10:56, Richard Bos wrote:
"Mehta Shailendrakumar " <sh************ *******@de.bosc h.com> wrote:
What is the significance of "malloc(0)" ?


Nearly nothing.
It doesn't return a NULL pointer, then what does it return?


In C89, "a unique pointer"; in C99, "as if the size were some nonzero
value, except that the returned pointer shall not be used to access an
object." In neither case is the pointer much use; you can't dereference
it, and its value is as good as random.
Where one can use such a "malloced" pointer?


Nowhere. Well, one can pass it to free() without worrying that this will
cause problems.
The only way in which this is useful is when you're not talking about
malloc(0) itself, but about realloc(0). That is a possible marginal case
in automatic allocation functions (e.g., for string handling), and in
such situations it's useful that it doesn't cause undefined behaviour.
That, AFAICT, is all.


I suppose it can at least be used with realloc.

Nov 14 '05 #4
Richard Bos wrote:
"Mehta Shailendrakumar " <sh************ *******@de.bosc h.com> wrote:
What is the significance of "malloc(0)" ?
Nearly nothing.
It doesn't return a NULL pointer, then what does it return?


In C89, "a unique pointer"; in C99, "as if the size were some nonzero
value, except that the returned pointer shall not be used to access an
object." In neither case is the pointer much use; you can't dereference
it, and its value is as good as random.
Where one can use such a "malloced" pointer?


Nowhere. Well, one can pass it to free() without worrying that this will
cause problems.


On some implementations won't it cause problems if the pointer returned
by malloc(0) is not free'd? (Probably those implementation where
malloc(0) does not return NULL.)
(...)


-Charlie

Nov 14 '05 #5
On Mon, 20 Jun 2005 07:54:55 -0700, Charles Mills wrote:
Richard Bos wrote:
"Mehta Shailendrakumar " <sh************ *******@de.bosc h.com> wrote:
> What is the significance of "malloc(0)" ?


Nearly nothing.
> It doesn't return a NULL pointer, then what does it return?


In C89, "a unique pointer"; in C99, "as if the size were some nonzero
value, except that the returned pointer shall not be used to access an
object." In neither case is the pointer much use; you can't dereference
it, and its value is as good as random.
> Where one can use such a "malloced" pointer?


Nowhere. Well, one can pass it to free() without worrying that this will
cause problems.


On some implementations won't it cause problems if the pointer returned
by malloc(0) is not free'd? (Probably those implementation where
malloc(0) does not return NULL.)


If a non-null value is returned from malloc, it should eventually be
free'd. It is possible (likely) that an implementation that returns a
non-null value from a malloc(0) will utilize a small amount of memory
which would not be available again until free was called with the return
value. Freeing a non-null value returned from malloc is always safe, if
malloc(0) return NULL, this does not, of course, need to be free'd.

Robert Gamble
Nov 14 '05 #6
Jean-Claude Arbaut wrote on 20/06/05 :
What is the significance of "malloc(0)" ?
I suppose it can at least be used with realloc.


In that case,

char *p = NULL

/* some loop */
p = realloc (p, size); /* intentionally simplied */

is the trick... It is similar to :

char *p = malloc (size);

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

Nov 14 '05 #7

"Mehta Shailendrakumar " <sh************ *******@de.bosc h.com> wrote

What is the significance of "malloc(0)" ?
It doesn't return a NULL pointer, then what does it return?
Where one can use such a "malloced" pointer?

There's a philosophical difference between nothing and emptiness. An example
in C is the null string (char *str = 0) and the empty string (char * str =
"").

Unfortunately C's malloc(0) doesn't make this distinction very well, because
it may return NULL or a pointer to no memory.

However were you to implement your own memory allocation scheme you could
work out the logic of the what the difference means for your program.
Nov 14 '05 #8
Mehta Shailendrakumar wrote:
Hi all,

What is the significance of "malloc(0)" ?
It doesn't return a NULL pointer, then what does it return?
Where one can use such a "malloced" pointer?


You may be able to use it as a tag of some sort, if you are generally
doing comparisons of pointers. Let's say you had a list of pointers to
data, but you wanted a few "special" values such as EMPTY and UNKNOWN.
Let's say your list node looks like this:

struct list_node
{
void *data;
struct list_node *next;
}

You could do the following:

void *EMPTY, *UNKNOWN;

int main()
{
EMPTY = malloc(0);
UNKNOWN = malloc(0);

...
}

void process_list (struct list_node *l)
{
struct list_node *cur;

for(cur = l; cur != NULL; cur = cur->next)
{
if(cur->data == UNKNOWN)
{
printf("Data Unknown\n");
}
if(cur->data == EMPTY)
{
printf("Data Empty\n");
}
else
{
process_data(cu r->data);
}
}
}

Anyway, any time you had a pointer to a certain type of data (maybe char
*), but needed some special values that aren't necessarily of that type
(like UNKNOWN or EMPTY above), you could malloc(0) to make sure that you
have a valid, unique pointer, but doesn't take up much memory.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #9
"Malcolm" <re*******@btin ternet.com> writes:
"Mehta Shailendrakumar " <sh************ *******@de.bosc h.com> wrote

What is the significance of "malloc(0)" ?
It doesn't return a NULL pointer, then what does it return?
Where one can use such a "malloced" pointer?
There's a philosophical difference between nothing and emptiness. An example
in C is the null string (char *str = 0) and the empty string (char * str =
"").


That's a misnomer; a null char pointer (char *str = 0) is not a string
at all. But yes, that's a good illustration of the difference between
not-a-string and an empty string.
Unfortunately C's malloc(0) doesn't make this distinction very well, because
it may return NULL or a pointer to no memory.


We have empty strings only because strings are terminated with a '\0',
so an empty string is not an empty array. C does not have empty
arrays, or zero-sized objects in general. malloc(0) should logically
produce a pointer to a zero-sized object, but since the language
doesn't really allow such a thing, the details are left up to the
implementation.

--
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.
Nov 14 '05 #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
6457
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
23338
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
2225
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
10771
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
2601
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
15751
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
2625
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
19165
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
2738
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
9926
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11096
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
10716
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...
1
10812
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7953
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
5780
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3223
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.