PDA

View Full Version : question about type casting


EscapeCharacter
10-12-2002, 01:02 PM
ive been cruising some usenet groups and came across some posts saying that typecasting what gets returned from the memory allocation function is redundant and shouldnt be used. they would do the following:

char *blah = malloc(sizeof(char)*LEN);

instead of what i normally see people do

char *blah = (char*)malloc(sizeof(char)*LEN);


so my question is does it really matter?

p.s. i would have asked on usenet but they would have probably just flamed me and called me a n00b, so i ask you nice people :)

bwkaz
10-12-2002, 01:59 PM
malloc() returns a (void *), and according to one C standard or another (I don't know specifics, but I do know it was a standard), a void pointer can be implicitly cast to anything.

At least, I think that's the case...

sans-hubris
10-12-2002, 02:23 PM
It doesn't matter, really. The only thing that omitting a typecast does is save you a few keystrokes.

EscapeCharacter
10-12-2002, 02:38 PM
Originally posted by sans-hubris
It doesn't matter, really. The only thing that omitting a typecast does is save you a few keystrokes.

thats funny cause the way people talk about it on comp.lang.c you would think they were talking about killing babies.

scanez
10-12-2002, 03:25 PM
AFAIK it doesn't really matter but using an explicit type cast is good coding style :)

sicarius
10-12-2002, 09:23 PM
Yeah as far as the language is concerned it doesn't matter either way. But it is the prefered style to cast (at least that is how the inventor of the language does it in his book on C).

Just FYI I believe it was ANSI C which first used void * as the generic pointer type. Prior to that it was char *.

Strike
10-14-2002, 12:44 PM
Yes, I believe that there's no rule binding anyone to cast the void * that malloc returns into what you want (since it will work as anything, since pointers are pointers are pointers). But, for code clarity, it's not a bad idea to include the cast. Those on c.l.c are probably the ones who are of the opinion that if you don't know that void pointers are the same as any other pointers, then you probably shouldn't be coding C (and I don't particularly like that attitude, personally).

darelf
10-14-2002, 02:16 PM
I think if you have all the warnings on your compiler turned on ( i.e. -Wall in GCC) you'll get warnings about it if you don't cast them.

But as everyone else has already said, it doesn't really matter.

sans-hubris
10-14-2002, 03:57 PM
Originally posted by darelf
I think if you have all the warnings on your compiler turned on ( i.e. -Wall in GCC) you'll get warnings about it if you don't cast them.

But as everyone else has already said, it doesn't really matter. Actually, no you won't. Since a void pointer has no type, there is no reason to give an error for any implicit casting you do on them. Check it out for yourself:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *x=malloc(sizeof(double)*20);
return 0;
}

stuka
10-14-2002, 04:09 PM
I know gcc will warn you about void pointers being used where other types are expected in some situations - printf is one of them.