Weekend topic: void* cast

Picky little unimportant topic:

I see casts to void * like, for ex.

char* cin;
cin=(char*)malloc(SOMETHING);
free((void *)cin); <<<< THIS ONE!!!

MS compiler never complains if you skip the last cast
and I noticed it because it strikes me as quite ugly.

I wonder if C ANSI-fication requires explicit cast to
void* like in free function above?
Or is it that some compilers issue unwanted warnings?

Tony

···

_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com

Tonko Juricic wrote:

Picky little unimportant topic:

I see casts to void * like, for ex.

char* cin;
cin=(char*)malloc(SOMETHING);
free((void *)cin); <<<< THIS ONE!!!

MS compiler never complains if you skip the last cast
and I noticed it because it strikes me as quite ugly.

I wonder if C ANSI-fication requires explicit cast to
void* like in free function above?
Or is it that some compilers issue unwanted warnings?

I think it used to be necessary in K&R C. In ANSI, void* is the
official "generic" pointer, and any assignment from or two one of
those is valid without a cast. Since malloc() and free() have
void* as their return resp. argument type, both casts in your
example are redundant.

That's how I understand it, anyway... :wink:

-schorsch

···

--
Georg Mischler -- simulations developer -- schorsch at schorsch com
+schorsch.com+ -- lighting design tools -- http://www.schorsch.com/

I believe this is correct, though I'd have to check a reference I
don't have to hand to be sure.

Randolph

···

On Sat, Oct 23, 2004 at 02:53:51PM -0400, Georg Mischler wrote:

Tonko Juricic wrote:

> Picky little unimportant topic:
>
> I see casts to void * like, for ex.
>
> char* cin;
> cin=(char*)malloc(SOMETHING);
> free((void *)cin); <<<< THIS ONE!!!
>
> MS compiler never complains if you skip the last cast
> and I noticed it because it strikes me as quite ugly.
>
> I wonder if C ANSI-fication requires explicit cast to
> void* like in free function above?
> Or is it that some compilers issue unwanted warnings?

I think it used to be necessary in K&R C. In ANSI, void* is the
official "generic" pointer, and any assignment from or two one of
those is valid without a cast. Since malloc() and free() have
void* as their return resp. argument type, both casts in your
example are redundant.

That's how I understand it, anyway... :wink: