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 13 2896
"Mehta Shailendrakumar" <sh*******************@de.bosch.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
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
On 20/06/2005 10:56, Richard Bos wrote: "Mehta Shailendrakumar" <sh*******************@de.bosch.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.
Richard Bos wrote: "Mehta Shailendrakumar" <sh*******************@de.bosch.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
On Mon, 20 Jun 2005 07:54:55 -0700, Charles Mills wrote: Richard Bos wrote: "Mehta Shailendrakumar" <sh*******************@de.bosch.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
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.
"Mehta Shailendrakumar" <sh*******************@de.bosch.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.
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(cur->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
"Malcolm" <re*******@btinternet.com> writes: "Mehta Shailendrakumar" <sh*******************@de.bosch.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_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.
Robert Gamble wrote: Freeing a non-null value returned from malloc is always safe,
s/non-null //
Christian
Robert Gamble <rg*******@gmail.com> wrote: On Mon, 20 Jun 2005 07:54:55 -0700, Charles Mills wrote:
Richard Bos wrote: "Mehta Shailendrakumar" <sh*******************@de.bosch.com> wrote:
> What is the significance of "malloc(0)"?
> 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.)
Theoretically any unfree()d allocated block could cause problems, but
there's no reason why malloc(0) should cause more problems than
malloc(sizeof int). On modern implementations, it shouldn't cause any
problems at all, except the obvious ones.
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.
True, but no more so than for malloc(anything_positive).
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.
But is still safe to be free()d, as much as a non-null value.
Richard
Jonathan Bartlett wrote: 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);
... }
<snip>
Of course, if malloc(0) returns NULL this does not help you, and it
could return NULL.
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.
malloc(0) could return a null pointer because the implementer decided
the easiest thing to do with malloc(0) was return a null pointer.
malloc(1) is only *likely* to fail if you are out of memory, although
the implementation *could* make it fail for other reasons.
So if you do a malloc(0) to get a unique pointer you have to test it if
you got a null pointer try malloc(1). Much simpler to just do a
malloc(1) and report a failure to the user and abort if you need unique
pointers, especially if they are being created on program start up as in
your example.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
On Mon, 20 Jun 2005 22:01:26 +0000, Keith Thompson wrote: "Malcolm" <re*******@btinternet.com> writes: "Mehta Shailendrakumar" <sh*******************@de.bosch.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.
More generally a string is a sequence of characters terminated by a null
character. A pointer is not a string but you can have a "pointer to a
string" which is terminology defined in the standard.
Lawrence This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
by: spasmous |
last post by:
main()
{
float * f;
initialize_f(f);
// ...use f for processing
free(f);
}
|
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...
|
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;...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |