Title: | C++ |
Notice: | Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS) |
Moderator: | DECCXX::AMARTIN |
Created: | Fri Nov 06 1987 |
Last Modified: | Thu Jun 05 1997 |
Last Successful Update: | Fri Jun 06 1997 |
Number of topics: | 3604 |
Total number of notes: | 18242 |
Can anybody explain results of the following program. It looks like a compiler bug to me. I tried it on osf with decxx v5.1 and v5.5. Symptoms are the same. There are two similar structures with the same fields but with a different order. If there is no public inheritence, the sizes are the same (16 byte). If I add inheritance (en empty class), sizes are different: Size of A is 24; Size of B is 16 Looks like the compiler tries to allocate at least 1 byte for the empty structure (?). You can get a byte difference, removing inheritance from one structure and using #pragma nomember_alignment (you will get 14 versus 13 byte). >> Serge ========================== #include <iostream.hxx> struct Sclass { }; typedef long unsigned SDataType; struct A : public Sclass { SDataType *addr; unsigned f1 : 1; unsigned f2 : 1; short unsigned f3; short unsigned f4; }; struct B : public Sclass { unsigned f1 : 1; unsigned f2 : 1; short unsigned f3; short unsigned f4; SDataType *addr; }; void main() { cout << "Size of A is " << sizeof(A) << "; Size of B is " << sizeof(B) << endl; }
T.R | Title | User | Personal Name | Date | Lines |
---|---|---|---|---|---|
3530.1 | SPECXN::DERAMO | Dan D'Eramo | Mon Apr 07 1997 15:15 | 25 | |
Topics 2362 and 3123 also discuss the affects of empty base classes on object layout. I'm guessing that your struct A is probably empty base class object (1 byte at offset 0) SDataType *addr (8 bytes at offset 8 for alignment) unsigned int bit fields f1, f2 (1 byte at offset 16) unsigned short f3 (2 bytes at offset 18 for alignment) unsigned short f4 (2 bytes at offset 20) padding for alignment (2 bytes at offset 22) and your struct B is probably empty base class object (1 byte at offset 0) unsigned int bit fields f1, f2 (1 byte at offset 1) unsigned short f3 (2 bytes at offset 4 for alignment) unsigned short f4 (2 bytes at offset 6) SDataType *addr (8 bytes at offset 8) With #pragma nomember_alignment both should be 13 without the one byte for the base class subobject and 14 with the one byte for the base class subobject. Dan | |||||
3530.2 | CADSYS::BOGDANOV | Mon Apr 07 1997 17:19 | 2 | ||
Thanks for the pointer to 2362. It explains a lot. | |||||
3530.3 | SPECXN::DERAMO | Dan D'Eramo | Mon Apr 07 1997 18:38 | 11 | |
> and your struct B is probably > > empty base class object (1 byte at offset 0) > unsigned int bit fields f1, f2 (1 byte at offset 1) > unsigned short f3 (2 bytes at offset 4 for alignment) > unsigned short f4 (2 bytes at offset 6) > SDataType *addr (8 bytes at offset 8) Actually, the starting offsets are probably 0,1,2,4,8 instead. Dan |