K
Keith Thompson
I can see no attack, merely an accurate statement of fact.
No, Dan, it's your opinion.
I can see no attack, merely an accurate statement of fact.
In said:No, Dan, it's your opinion.
In said:If I was working for Dan and he wanted to modify input parameters, so be
it.
Dan Pop said:In <[email protected]>
All I want from a programmer is to do whatever makes most sense under the
specific circumstances, rather than using a system of *inflexible* rules.
Come to think of it, my own inflexible rules are:
1. Identifiers must make as much sense as possible in as few characters
as possible.
2. Code must be indented (the style doesn't matter).
3. Dereferencing a function pointer must not look like an ordinary
function call.
4. Macros should be spelled in upper case (with the possible exception
of those having the same semantics as a function call).
5. typedef is not a typing saving device.
6. Reserve implicit comparisons against 0 for conceptual booleans.
7. If a function can fail, don't ignore checking its success, unless there
is a good reason for that.
8. Don't use any declaration more complex than the declaration of a
pointer to function or pointer to array. For anything more complex
than that, use typedefs.
Dan said:2. Code must be indented (the style doesn't matter).
Emmanuel said:
tweak said:I went to the faq and it looks like:
void f(ip)
int *fp;
{
static int dummy = 5;
ip = &dummy;
}
shouldn't the above read:
int *fp;
void f(ip) {
static int dummy = 5;
ip = &dummy;
}
Thanks for correcting me. I've never seen the style before.Keith said:Not quite. It actually looks like this:
void f(ip)
int *ip;
{
static int dummy = 5;
ip = &dummy;
}
There is no "fp".
The code fragment uses an old-style function declaration, which is
a bit odd. I'd prefer this:
void f(int *ip)
{
static int dummy = 5;
ip = &dummy;
}
Um, no.
Keith Thompson said:[...]tweak said:I went to the faq and it looks like:
void f(ip)
int *fp;
{
Not quite. It actually looks like this:
void f(ip)
int *ip;
{ [...]
The code fragment uses an old-style function declaration, which is
a bit odd.
I went to the faq and it looks like:
void f(ip)
int *fp;
{
static int dummy = 5;
ip = &dummy;
}
P.S. The FAQ really does answer the question even
though the poster thought their problem
was malloc(), which had the wrong size_t --
cutting off '\0'.
Note: The only item I can find regarding casting
of malloc() is K&R2 pg. 142:
"In C, the proper method is to declare that
malloc return a pointer to void, then
explicitly coerce the pointer into the
desired type with cast."
Now, I couldn't find anything specific in
ISO C99, so I guess it's up to the programmer
to decide.
Darrell Grainger said:I guess "Why is casting the result of malloc a bad thing?" would make for
a good FAQ.
Thanks.pete said:tweak wrote:
Check the errata:
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
142(§6.5, toward the end): The remark about casting the return value
of malloc ("the proper method is to declare ... then
explicitly coerce") needs to be rewritten. The example is correct
and works, but the advice is debatable in the context of the
1988-1989 ANSI/ISO standards. It's not necessary
(given that coercion of void * to ALMOSTANYTYPE * is automatic),
and possibly harmful if malloc, or a proxy for it,
fails to be declared as returning void *.
The explicit cast can cover up an unintended error.
On the other hand, pre-ANSI, the cast was necessary,
and it is in C++ also.
This FAQ has been beaten to death in clc. Ben Pfaff has provided a VGA
(Very Good Answer) at http://benpfaff.org/writings/clc/malloc-cast.html.
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.