why Struct size does not match the data inside?
Unsolved
General and Desktop
-
Hi,
I have a hardware that would accept Some commands, and the commands should have a specific size,struct SamplingCard{ short cmd; uint32_t CapDelay; uint32_t CapCnt; }; union { SamplingCard Card; char array[1460]; } command; QByteArray wBa; wBa = QByteArray::fromRawData(command.array, sizeof(SamplingCard));
The problem is that I need the QbyteArray to be exactly 1460 bytes, But it is 12, also the size of struct should be 10Bytes, But Qt would consider it 12 bytes! any work around this?
-
This has nothing to do with Qt.
The struct is padding to respect alignment, use#pragma pack
if you want to get rid of it:#include <QDebug> #include <QByteArray> #pragma pack(2) struct SamplingCard{ qint16 cmd; quint32 CapDelay; quint32 CapCnt; }; int main() { SamplingCard command{1,2,3}; QByteArray wBa(1460,0); std::memcpy(wBa.data(),&command,sizeof(SamplingCard)); qDebug().noquote() << wBa.toHex(); return 0; }
-
Keep in mind that #pragma controlls implementation defined behavior, and isn't subject to official standardization. If the development environment doesn't match the deployment environment, don't forget to check both.