g++ may fail to compile __packed structures

Pavlin Radoslavov pavlin at icir.org
Mon Sep 6 13:24:15 PDT 2004

It appears that the lastest g++ compiler that comes with FreeBSD may
fail to compile a __packed structure when one of its fields is
passed by reference. At the end of this email is a sample program
that demonstrates the problem. The compilation error is:

pavlin at carp[14] g++34 test.cc
test.cc: In function `int main()':
test.cc:22: error: cannot bind packed field `f1.foo::f' to `int&'
Exit 1

The problem appears to exist only with the recent g++ compiler that
comes with FreeBSD:

pavlin at carp[15] g++34 --version
g++34 (GCC) 3.4.2 20040806 (prerelease) [FreeBSD]

E.g., the problem doesn't exist with older compilers like 3.3.x from
FreeBSD or the vanilla g++-3.4.1 from http://gcc.gnu.org/.
It is not even in 3.4.0 from FreeBSD:
("g++34 (GCC) 3.4.0 20040310 (prerelease) [FreeBSD]")


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct foo {
    int f;
} __packed;

copy_out(int& i)
    i = 0x11223344;

    struct foo f1;

    memset(&f1, 0, sizeof(f1));
    // copy_out(reinterpret_cast<int&>(f1.f));


More information about the freebsd-current mailing list