This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Packed structs and C++

When we pack a structure, the type of the packed fields does
not become 'attribute((unaligned)) T'. this causes problems with
taking their address and that happens 'invisibly' with passing by
reference. Here is some example code.

struct Foo {
 char c;
 int i __attribute__((packed));

typedef map<int,int> Map_t;

void Foo (Map_t &m, int from, int to)
  Foo f;
  f.i = from;

#if 1
  m.insert(map::value_type(f.i, to)); // fails
  int t = f.i;
  m.insert(map::value_type(t, to)); // works
  m.find (from); // will it be found?

why does this happen? Well, map::value_type (f.i, to) calls
	pair<int, int>::pair (L const &, R const &);
which implicitly takes the address of f.i, and loses the unalignedness
information. This can lead to bus errors or silently reading the wrong
memory locations.

do nothing. I think this is unacceptable. In C, it is more obvious when
you are taking the address of something, in C++ it happens much more

packed fields have an unaligned type, and that is propagated through
the type system just as cv-qualifiers are. This will also work for
C, and give allow the user to specify when unaligned objects might be
pointed to. Template deduction will probably have to start deducing
attributes -- bleah!

packed fields are like bitfields. Thus they cannot have their address
taken and they can bind to 'T const &' parameters by invoking
the copy constructor. If they cannot have their address taken, do
we want offsetof (M, packedField) to work or not?


Nathan Sidwell    ::   ::     CodeSourcery LLC
         The voices in my head said this was stupid too    ::

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]