Re: RE: [LIP] alloc functions and stdlib...

Binand Sethumadhavan <[email protected]>
Newsgroups gmane.user-groups.linux.india.programmers
Message-ID <[email protected]>
I usually don't reply to messages with disclaimers, but since the list
admin himself is looking the other way, let us ignore it just this once ;)

Sameer Oak posted in linux-india-programmers:
> including stdlib.h is a good idea. but is it good enough over including
> malloc.h?  i was particularly looking for this distinction between stdlib.h
> and malloc.h nothing is commented on malloc.h over stdlib.h.

stdlib.h is part of ANSI C, malloc.h is not. I'd say stdlib.h is certainly
a better idea than malloc.h, if you value the portability of your code.

> if stdlib.h is not included we need to type cast the return value of
> malloc(), calloc(), realloc().
> if not type casted, the compiler generates a warning:
> warning: assignment makes pointer from integer without a cast.

That's right. The compiler assumes a return value of type int for any
unprototyped function.

On the other hand, once you have stdlib.h included, the compiler knows
malloc() returns a void *, and since it is legal in C to convert a void *
to any other pointer type, you can assign the return value of malloc()
to any pointer type without a cast.

Binand

-- 
[[email protected] root]# uptime
11:55am  up 58 days,  4:49,  2 users,  load average: 1023.78, 419.92, 158.90


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.