[LCP]sizeof question.

David filiond at videotron.ca
Fri Sep 20 21:51:01 UTC 2002


Greg Black wrote:
> 
> ianezz at sodalia.it wrote:
> 
> | Il clone di Greg Black ha segretamente scritto:
> |  > David wrote:
> |  >
> |  > |  struct _Options *ptr;
> |  > |
> |  > |  ptr = (struct _Options *) calloc (1, sizeof *ptr);
> |  >
> |  > Lose the cast ^^^^^^^^^^^^^^^^^^^ -- it's wrong.
> |
> | Uh?
> |
> | [...]
> |
> | calloc() returns a void *, so in turn one needs a cast.
> |
> | What is the problem?
> 
> Read the Standard.  You'll see that the cast is not needed.  Use
> of unneeded casts is at best unwise (wrong, as far as I'm
> concerned) because it can mask real errors from the compiler.
> This has been covered on this list before.
> 
> The rule is simple -- none of the malloc family requires a
> cast.  Very few casts are required in Standard C and most uses
> that people make of casts are ill-advised.
> 
> If this is still confusing, read a good book on C (but be aware
> that at least 95% of all books on C are pathetic).
> 
> Greg
> _______________________________________________

I added the case because the compiler complains if I don't.
SUN Workshop, not gcc :(, unfortunaltly.

-- 
David Filion		filiond[at]videotron[dot]ca
Linux counter #195143

"Yes, it's manly, but let's face it, so is bungee-jumping with the
 cord tied to your testicles." -- Linus Torvalds



More information about the linuxCprogramming mailing list