www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/03/05:50:47

Message-Id: <2.2.32.19970303150304.00689f6c@mailhost>
Mime-Version: 1.0
Date: Mon, 03 Mar 1997 13:03:04 -0200
To: djgpp AT delorie DOT com
From: Eyal Ben-David <eyalb AT aks DOT com>
Subject: Re: quick malloc question

>Why the HELL do people keep writing things like (char *)malloc(x)????????
>
>GCC, and to my knowledge other compilers, allow any pointer to be assigned
>to a variable of type void * and conversely allow any variable of type void
>* to be assigned to any pointer, without casts.
>

`malloc' returns void* while p can be of any pointer type. You are actually
casting from  (void*) to (char*).

In C it is not a problem, the compiler does the conversion for you with or
without warnings (depends on the compiler and warning level).

In C++ it is a compilation error so you must explicitly cast from one
pointer type to another (except when the target type is void*).


Eyal. 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019