On 12 November 2006 03:35, Howard Chu wrote:
> Here's a different example, which produces the weaker warning
> warning: type-punning to incomplete type might break strict-aliasing rules
>
> struct foo;
>
> int blah(int fd) {
> int buf[BIG_ENOUGH];
> void *v = buf;
> struct foo *f;
>
> f = v;
> f = (struct foo *)buf;
>
> init(f, fd);
> munge(f);
> flush(f);
> }
>
> "foo" is an opaque structure. We have no idea what's inside, we just
> know that it's relatively small. There are allocators available that
> will malloc them for us, but we don't want to use malloc here because
> it's too slow, so we want to reserve space for it on the stack, do a few
> things with it, then forget it.
>
> If we go through the temporary variable v, there's no warning. If we
> don't use the temporary variable, we get the "might break" message.
Try
> f = (struct foo *)(void *)buf;
Or even better...
struct foo;
int blah(int fd) {
struct foo *f;
f = alloca (BIG_ENOUGH);
init(f, fd);
munge(f);
flush(f);
}
cheers,
DaveK
--
Can't think of a witty .sigline today....