g++ may fail to compile __packed structures

From: Pavlin Radoslavov <pavlin_at_icir.org>
Date: Mon, 06 Sep 2004 13:24:15 -0700
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]")

Thanks,
Pavlin


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

struct foo {
    int f;
} __packed;

void
copy_out(int& i)
{
    i = 0x11223344;
}

int
main()
{
    struct foo f1;

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

    exit(0);
}
Received on Mon Sep 06 2004 - 18:24:15 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:10 UTC