Padding in C++ structures

bridged with qnx.development_tools
Post Reply
John Nagle

Padding in C++ structures

Post by John Nagle » Sat Dec 11, 2004 12:44 am

[QNX 6.21PE]

The size of this structure is 24 bytes. It should be 20.
The offset of the last field, m_valuecount, is 18 bytes.
This is plain data, no constructor, no member functions.
Why is there padding?

Adding a "uint32_t" at the end, to round it up to a
multiple of 8 bytes, makes it even bigger. It's not a
rounding issue.

Compile options are

/usr/bin/qcc -Vgcc_ntox86 -lang-c++ -g

John Nagle
Team Overbot

struct LidarScanLineHeader {
uint64_t m_timestamp; float m_tilt;
uint8_t m_sensorid; uint8_t m_statusByte; uint8_t
m_scanIndex; uint8_t m_unused1; uint16_t
m_unused2; uint16_t m_valueCount; };

John Garvey

Re: Padding in C++ structures

Post by John Garvey » Sat Dec 11, 2004 12:50 am

John Nagle wrote:
[QNX 6.21PE]

The size of this structure is 24 bytes. It should be 20.
The offset of the last field, m_valuecount, is 18 bytes.
This is plain data, no constructor, no member functions.
Why is there padding?
I would say it is "in case you have an array of them". So
overall 8-byte alignment is needed so that "blah[1].m_timestamp"
is not unaligned.

You can wrap it within a #include <_pack1.h> / <_packpop.h>
or "__attribute__((packed))" it to force it down to 20 bytes,
if you know it will never be used as an array.

Post Reply

Return to “qnx.development_tools”