Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > call to malloc with size 0

Reply
Thread Tools

call to malloc with size 0

 
 
Neo
Guest
Posts: n/a
 
      11-17-2004
Hi Folks,

I've a simple qestion related to dynamic memory allocation in C here is the
code:

#include <stdio.h>

int main()

{

char *p;

if( (p=(char*)malloc(0)) == NULL)

printf("NULL\n");

else

printf("Valid Pointer\n");

return 0;

}



Output : "Valid Pointer"

Why this code fragment returns a valid pointer to a memory block???

-Neo




 
Reply With Quote
 
 
 
 
Peter Smithson
Guest
Posts: n/a
 
      11-17-2004
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)
says...

> Why this code fragment returns a valid pointer to a memory block???
>
> -Neo


I've seen this before. Some versions of UNIX will return an error,
others will return a valid pointer. I'm not sure which is correct but I
found the 2nd situation to be more common and more useful as you don't
have to code round it - I've got a few +1's in some code just to
guarentee a valid pointer which happens to contain nothing. That way I
know the pointer has been initialised even it doesn't contain anything.
It's working like a flag as well as a place to store results.

--
http://www.beluga.freeserve.co.uk
 
Reply With Quote
 
 
 
 
Ravi Uday
Guest
Posts: n/a
 
      11-17-2004

"Neo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Folks,
>
> I've a simple qestion related to dynamic memory allocation in C here is

the
> code:
>
> #include <stdio.h>
>
> int main()
>
> {
>
> char *p;
>
> if( (p=(char*)malloc(0)) == NULL)
>
> printf("NULL\n");
>
> else
>
> printf("Valid Pointer\n");
>
> return 0;
>
> }
>
>
>
> Output : "Valid Pointer"
>
> Why this code fragment returns a valid pointer to a memory block???
>
> -Neo
>
>

See what the standard says

4.10.3 Memory management functions:

If the size of the space requested is zero, the behavior is
*implementation-defined*; the value returned shall be either a null
pointer or a unique pointer.

So check in your impelementation documents for answer !

- Ravi



>
>



 
Reply With Quote
 
Neo
Guest
Posts: n/a
 
      11-17-2004

"Ravi Uday" <(E-Mail Removed)> wrote in message
news:1100686594.274808@sj-nntpcache-5...
>
> "Neo" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi Folks,
>>
>> I've a simple qestion related to dynamic memory allocation in C here is

> the
>> code:
>>
>> #include <stdio.h>
>>
>> int main()
>>
>> {
>>
>> char *p;
>>
>> if( (p=(char*)malloc(0)) == NULL)
>>
>> printf("NULL\n");
>>
>> else
>>
>> printf("Valid Pointer\n");
>>
>> return 0;
>>
>> }
>>
>>
>>
>> Output : "Valid Pointer"
>>
>> Why this code fragment returns a valid pointer to a memory block???
>>
>> -Neo
>>
>>

> See what the standard says
>
> 4.10.3 Memory management functions:
>
> If the size of the space requested is zero, the behavior is
> *implementation-defined*; the value returned shall be either a null
> pointer or a unique pointer.
>
> So check in your impelementation documents for answer !
>
> - Ravi
>


O'kay thanks for pointing it out.
But what should be the suggested behavior and why?
Is a valid pointer right way?
I think NULL is a better option? isn't it???

-Neo


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      11-17-2004
On Wed, 17 Nov 2004 15:49:10 +0530, Ravi Uday wrote:

>
> "Neo" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi Folks,
>>
>> I've a simple qestion related to dynamic memory allocation in C here is

> the
>> code:
>>
>> #include <stdio.h>
>>
>> int main()
>>
>> {
>>
>> char *p;
>>
>> if( (p=(char*)malloc(0)) == NULL)


Get rid of the cast. The compiler should then complain, which is
good because the code is broken. Adding the cast doesn't fix the
code, it just stops the compiler complaining about the problem.

The problem is that you don't have a valid declaration for malloc()
in scope when you call it. In that case the compiler assumes
that malloc() returns int, but it doesn't, it returns void *. All
standard library functions are provided with a declaration in
the appropriate standard header. For malloc() that is <stdlib.h>.

>>
>> printf("NULL\n");
>>
>> else
>>
>> printf("Valid Pointer\n");
>>
>> return 0;
>>
>> }
>>
>>
>>
>> Output : "Valid Pointer"
>>
>> Why this code fragment returns a valid pointer to a memory block???


Your code invokes undefined behaviour due to the invalid cast, so anything
can happen. Once that's fixed the issue is as below i.e. the compiler does
it because the standard says it can.

>>
>> -Neo
>>
>>

> See what the standard says
>
> 4.10.3 Memory management functions:
>
> If the size of the space requested is zero, the behavior is
> *implementation-defined*; the value returned shall be either a null
> pointer or a unique pointer.
>
> So check in your impelementation documents for answer !


Lawrence
 
Reply With Quote
 
Method Man
Guest
Posts: n/a
 
      11-17-2004

"Ravi Uday" <(E-Mail Removed)> wrote in message
news:1100686594.274808@sj-nntpcache-5...
>
> "Neo" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi Folks,
> >
> > I've a simple qestion related to dynamic memory allocation in C here is

> the
> > code:
> >
> > #include <stdio.h>
> >
> > int main()
> >
> > {
> >
> > char *p;
> >
> > if( (p=(char*)malloc(0)) == NULL)
> >
> > printf("NULL\n");
> >
> > else
> >
> > printf("Valid Pointer\n");
> >
> > return 0;
> >
> > }
> >
> >
> >
> > Output : "Valid Pointer"
> >
> > Why this code fragment returns a valid pointer to a memory block???
> >
> > -Neo
> >
> >

> See what the standard says
>
> 4.10.3 Memory management functions:
>
> If the size of the space requested is zero, the behavior is
> *implementation-defined*; the value returned shall be either a null
> pointer or a unique pointer.
>
> So check in your impelementation documents for answer !
>
> - Ravi
>


<OT>

Why didn't they put it in the standard that a null pointer must be returned?
It seems like the right/logical behaviour for calling malloc on size 0 and
avoids any confusion.

</OT>


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-17-2004
Peter Smithson <(E-Mail Removed)> wrote:

> In article <(E-Mail Removed)>, (E-Mail Removed)
> says...
>
> > Why this code fragment returns a valid pointer to a memory block???

>
> I've seen this before. Some versions of UNIX will return an error,


By which, I hope, you mean a null pointer, since causing a segfault or a
signal for malloc(0) is not conforming.

> others will return a valid pointer. I'm not sure which is correct


Either.

Richard
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      11-17-2004
On Wed, 17 Nov 2004 07:45:50 -0500, Method Man wrote:


....

> <OT>
>
> Why didn't they put it in the standard that a null pointer must be returned?
> It seems like the right/logical behaviour for calling malloc on size 0 and
> avoids any confusion.
>
> </OT>


Probably because there were existing implementations for both types of
result. I don't think it makes much practical difference, I can only think
of 2 uses for this:

1. Creating a unique pointer value which compares unequal to all others.
For this to work you would need to prefer the non-null return behaviour.
Of course you can use malloc(1) for this. However malloc() can always fail
so it could return null for any call.

2. You are setting up an object whick you might extend later using
realloc(). However whether malloc(0) return null or not is immaterial to
this, either result can be passed to realloc(). So the latitude given by
the standard is harmless here. Of course you have the issue of freeing the
"object" for a non-null return.

Lawrence
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-17-2004
Lawrence Kirby <(E-Mail Removed)> wrote:

> 2. You are setting up an object whick you might extend later using
> realloc(). However whether malloc(0) return null or not is immaterial to
> this, either result can be passed to realloc(). So the latitude given by
> the standard is harmless here. Of course you have the issue of freeing the
> "object" for a non-null return.


Even that is not a problem, since free(0) is harmless. No matter what
you get from malloc(), you can free() it.

Richard
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      11-17-2004
On Wed, 17 Nov 2004 13:53:09 +0000, Richard Bos wrote:

> Lawrence Kirby <(E-Mail Removed)> wrote:
>
>> 2. You are setting up an object whick you might extend later using
>> realloc(). However whether malloc(0) return null or not is immaterial to
>> this, either result can be passed to realloc(). So the latitude given by
>> the standard is harmless here. Of course you have the issue of freeing the
>> "object" for a non-null return.

>
> Even that is not a problem, since free(0) is harmless. No matter what
> you get from malloc(), you can free() it.


True, my intent was more along the lines of you have consider calling free()
appropriately for a non-null return, whereas if the return was always
null in this case you wouldn't have to worry.

However given the possibility extending the object to non-zero length
there's likely to be code to handle freeing anyway, so it probably turns
out to be a non-issue.

Lawrence


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments