cast to malloc 
Author Message
 cast to malloc

Hi
 I have seen a lot of people in this newsgroup advising to remove the cast
to a malloced memory.
 Can you please explain why it is not advisable to cast malloc's return
value ?
  malloc returns void *, so isnt it better to cast  it into whatever we want
?

Thanks
 Mahesh



Mon, 31 Jan 2005 04:47:22 GMT  
 cast to malloc

Quote:

>  I have seen a lot of people in this newsgroup advising to remove the
> cast to a malloced memory.
>  Can you please explain why it is not advisable to cast malloc's
> return value ?

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.

Quote:
>   malloc returns void *, so isnt it better to cast it into whatever we
> want ?

Your consequence does not follow from your premise.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield


Mon, 31 Jan 2005 04:58:43 GMT  
 cast to malloc
On Wed, 14 Aug 2002 13:47:22 -0700, in comp.lang.c , "MC"

Quote:

>Hi
> I have seen a lot of people in this newsgroup advising to remove the cast
>to a malloced memory.
> Can you please explain why it is not advisable to cast malloc's return
>value ?

See ben's answer for a lucid explanation

Quote:
>  malloc returns void *, so isnt it better to cast  it into whatever we want?

No. The C Standard defines void* as being convertible  into any other
object pointer type, and the compiler knows precisely how to do it all
on its own. There's no need to give it an extra hint, and indeed it
can generate errors as Ben explained.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Mon, 31 Jan 2005 07:00:26 GMT  
 cast to malloc

Quote:

>* Casting its return value can mask a failure to #include
><stdlib.h>, which leads to undefined behavior.

[This is for the benefit of the OP, not an attempt to correct Ben]

More specifically, the absence of a correct declaration for malloc
causes this problem. When the compiler sees the call to malloc (a
function it has never heard of before) it is required to assume that
the return type of this new function is int. When malloc returns, the
calling code attempts to fetch an int value from wherever it expects
such a return value to be. Since there was no int return value (only a
void *), and there's no guarantee that void pointers are returned the
same way as ints, it may fetch a completely incorrect value. This
garbage value is then converted to a pointer (thanks to the
superfluous cast) and stored into the pointer variable on the
left-hand side of the assignment. Obviously, this is a big problem.

This is just an example of how it might fail. The bottom line is that
having the wrong return type for malloc results in undefined behavior
- anything could happen. It could work today and format your hard
drive tomorrow.

Without the cast, the compiler has to complain about an apparent
attempt to assign an int to a pointer. This gives the programmer a
chance to fix the problem by providing a correct declaration for
malloc (most likely by #includeing stdlib.h).

-Kevin



Mon, 31 Jan 2005 10:02:25 GMT  
 cast to malloc
Ben, Kevin and Mark
 Thanks for your replies.
  I really appreciate that.

 Mahesh

Quote:
> Hi
>  I have seen a lot of people in this newsgroup advising to remove the cast
> to a malloced memory.
>  Can you please explain why it is not advisable to cast malloc's return
> value ?
>   malloc returns void *, so isnt it better to cast  it into whatever we
want
> ?

> Thanks
>  Mahesh



Tue, 01 Feb 2005 01:00:29 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. malloc: to cast or not to cast

2. casting malloc question.

3. casting malloc question.

4. never cast malloc?

5. casting return value from malloc

6. type casting malloc() to void *

7. Why not cast the rerurn from malloc()?

8. More about casting malloc()...

9. About casting malloc()...

10. Type casting malloc??

11. Casting malloc pointer...

12. Casting malloc() and calloc()

 

 
Powered by phpBB® Forum Software