Mail Archives: djgpp/1999/07/22/00:56:53
> Edward Hill wrote:
>
> > It's good practice not to cast, malloc doesn't require a cast and a cast
> > can sometimes mask errors.
>
> How can casting mask an error? And why doesn't malloc need a cast?
Casting can mask an error because it defeats type safety.
This is legal:
char* b = "Hello.";
int a = (int)b;
It's probably not a good idea, but the cast lets you get away with it.
It masks the error.
Malloc doesn't need a cast in C since C doesn't complain about
converting from a void* to any other kind of pointer. C++ does since it
needs to know what to convert from and will issue a warning if you
convert from void* (besides, you should use "new" in C++, which returns
the specific kind of pointer you want anyway--no void* there). In C,
the cast is a waste of typing.
- Raw text -