Pedants

W

Walter Roberson

(e-mail address removed)-cnrc.gc.ca (Walter Roberson) wrote:
Why? Would you normally do
int i='\0';
rather than
char c='\0';
int i=0;
void *p=NULL;

Someone might (but *I* probably wouldn't), if i represented a character.
For example,

int i='\0';
if (somecondition) {
i = getchar();
}
if (i == EOF) {
/* whatever */
}

That is, Best Practice is to use an int to store any value that
is a char that has to be read in by getchar(), getc() or fgetc()
(because you need to be able to post-check against the int value EOF).
If someone were initializing that int and they wanted to emphasize the
char-in-int's-clothing nature of the variable, they just might
choose to initialize with '\0'... or might get confused about the
meaning of NULL and try to initialize with that.
 
R

Richard Bos

Kaz Kylheku said:
Casting malloc is just fine.

Casting malloc() is a grave mistake, as bad as making a bottle of apple
juice carry a dangerous substance sign.
In fact, it's useful in a C compiler to have C++-style type checking
for void *: that is to say, implicit conversions from void * to some
other pointer type being diagnosed.

No, it's not. In C, those conversions are correct, and with good reason.
Getting spurious warnings for them is a bother, and makes you ignore
real warnings. Casting away those spurious warnings is also a bother,
and makes you a. ignore real casts and b. cast away other, important,
warnings.
The rationale for /not/ casting malloc is outdated, based on the idea
that compilers allow functions to be called without a prior
declaration, without issuing a diagnostic.

That's only part of the rationale, and not the most important part.
Code reviewers have to look not only for bugs, but for adherence to
coding conventions (and be pedantic about that just as much as
language issues).

And to look for dangerous code.

Spurious casts are dangerous. Not always to the program - but nearly
always to the program_mer_. That's the real reason to avoid them. Casts
should make you sit up and think "Hey, a cast! What is going on here?",
not "Oh, *yawn* another cast, must be more C++-avoidance hackery".

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top