Quote:
Originally Posted by
robwhit
> void* can be anything - it can be int*, it can be be double**, even your_own_struct*****. Therein lies the danger.
No, there lies the genericity. People know what they're dealing with then they use a void pointer. It's a generic pointer.
Therein lies the danger. It's generic. The compiler has no knowledge of the type it contains.
A function that accepts a generic point can be difficult since you don't know type, size or how it looks like.
Prone to mistakes. Bad for generic code.
Quote:
>Therefore, it is good that the compiler complains or warns when convert from void* to something else.
An explicit cast tells the compiler you know what you're doing.
But the compiler already knows what you're doing. It's just a matter of implicit versus explicit. As far as I am concerned, you made the choice when you chose the type of void pointer.
But people make mistakes, you know.
How is the compiler supposed to know you made it intentionally or if it was a mistake?
Quote:
> The problem is that this behavior still exists and some compilers do not complain at all.
In that case, a compile with another compiler or a static analysis tool should fix it.
I'm not sure I like that sound of that, though.
But if you say it's not a problem, then I say cast the return of malloc. It's good practice.
Quote:
> Should all compilers support it, then yes, it would not be a problem and the return should be cast.
What compilers do not warn about implicit function declarations?
I don't know, I'm just echoing the FAQ, and various other members on the board stating that casting the return of malloc is bad™.
Quote:
> IMHO, yes it does. Stricter type security for one.
And what benefit does being stricter give? You already declared that you want to use a generic pointer.
A generic pointer can be anything, so the compiler is correct to be cautious.
It stops silly mistakes and it prevents some nonsensical code from compiling (I know you're going to want examples, right?).
Quote:
I don't see any logic in there. I see a different argument.
How so?