[Freeciv-Dev] Re: (PR#12511) can't cast typed pointer as void* in freelo
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12511 >
On Mon, Mar 14, 2005 at 04:44:58PM -0800, Jason Short wrote:
> When compiling with 'strict' c99 I get some errors in freelog statements
> when matching a typed pointer with a %p (void pointer) printf symbol.
Exact. The C99 standard states:
p The argument shall be a pointer to void. The value of the pointer is
converted to a sequence of printable characters, in an implementation-
defined manner.
This actually isn't specific to C99 at all; it's been part of C89 and
ANSI as well, and is included in K&R, 2nd ed.
> suspect that NOT using a cast here could cause problems under some
> "potential" c99-compliant compilers. I also suspect no such compilers
> exist.
The problem is that printf expects %p to indicate that there's a void*
on the stack, but the compiler may not be aware of that, since printf is
var_args (and thus doesn't have a strict prototype), so it may not do
the cast automatically. Since there is no guarantee that void* and T*
have the same representation, the Earth may explode (especially if they
don't take the same size on the stack).
Now, gcc is smart enough to know that, so it flags any non-void pointer
passed to printf. It's actually even smarter, and will peek into
printf's format string, recognize "%p", and cast accordingly. But such
level of smartness is certainly no requirement of a C compiler.
(FYI, experts disagree on whether or not you actually need to cast char*
to void*, since they have the same representation.)
> Also this doesn't include any client changes. The client doesn't get
> very far with c99-pedantic compilation.
Amen.
--
Frédéric Brière <*> fbriere@xxxxxxxxxxx
=> <fbriere@xxxxxxxxxx> IS NO MORE: <http://www.abacomsucks.com> <=
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] Re: (PR#12511) can't cast typed pointer as void* in freelog?,
Frédéric Brière <=
|
|