Re: Inconsistent Program Results
- From: dj3vande@xxxxxxxxxxxxxxxxxxxxxxxxxxxx (Dave Vandervies)
- Date: Thu, 8 Mar 2007 19:10:55 +0000 (UTC)
In article <1173360070.950646.19410@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
John Bode <john_bode@xxxxxxxxxxx> wrote:
On Mar 8, 2:08 am, Francine.Ne...@xxxxxxxxxxxxxx wrote:
malloc(), if it succeeds, returns a pointer (of type void*) to a chunk
of memory that's guaranteed to be properly aligned for any data type.
A void* pointer can be implicitly converted to any pointer-to-object
type. The cast is absolutely unnecessary.
OK then, I'll try to remember not to typecast malloc in future, though
again it seems very common in all the code I've seen.
<reiterates prior point about 90% of C references being crap>
Very old implementations of C (pre-ANSI, C89) define malloc() as
returning char*, and in those implementations the cast *was*
necessary, since you can't implicitly convert a char* to another
object pointer type. It could be that the author of your text never
made it past 1985; however, just because he's living in the past
doesn't mean you should too.
[...]
If you're getting all of this from your textbook, throw it in the
trash RIGHT NOW and get something authoritative ("The C Programming
Language", 2nd ed., Kernighan & Ritchie, is about as authoritative as
it gets; a good companion is "C: A Reference Manual", 5th ed.,
Harbison & Steele).
It's worth noting that K&R2 does tell you to cast malloc.
They have, however, fixed that in the errata (which, for any technical
book, is the first thing you should go looking for after you buy the
book).
They also at least had a good reason - void * was new at the time they
wrote the book, so they were used to casting the char * that came
from malloc, and they tested their code with a C++ compiler (which
also requires the cast) because of a lack of C compilers with the
new-at-the-time prototype checking and other features.
Any C book written since, oh, around 1992 that casts malloc is Wrong
With No Good Reason; the authors should have had a compiler that didn't
require the cast, though they at least may have had the excuse of old
habits to fall back on. *Any* book that declares void main is Wrong
With No Excuse, since it was never necessary or even legal (void was
introduced in C89, which specifically states that main must return int).
dave
--
Dave Vandervies dj3vande@xxxxxxxxxxxxxxxxxxx
Two glaring problems and one subtle error is a better-than-average
outcome in these parts.
--Eric Sosman in comp.lang.c
.
- References:
- Inconsistent Program Results
- From: Francine . Neary
- Re: Inconsistent Program Results
- From: Keith Thompson
- Re: Inconsistent Program Results
- From: Francine . Neary
- Re: Inconsistent Program Results
- From: John Bode
- Inconsistent Program Results
- Prev by Date: Re: Casting of void pointer returned by malloc()
- Next by Date: Re: symbolic computation in c language !!!
- Previous by thread: Re: Inconsistent Program Results
- Next by thread: Re: Inconsistent Program Results
- Index(es):
Relevant Pages
|