__attribute__((packed)) misuse?
May 7, 2014 at 9:21pm UTC
I'm trying to limit the size of struct/enum by means of the __attribute__((packed)):
1 2 3 4 5 6 7 8 9
enum LogIdentifier
{
LogIdentifierHouseKeeping = 0x01,
LogIdentifierMagnetometer = 0x02,
LogIdentifierDiffMIPS = 0x03,
LogIdentifierMIPS = 0x04,
LogIdentifierDiffSMDH = 0x05,
LogIdentifierSMDH = 0x06
} __attribute__((packed));
1 2 3 4 5
enum DataSegmentMIPS {
Counters_00_20 = 0x01,
Counters_21_50 = 0x02,
Counters_51_80 = 0x04
} __attribute__((packed));
1 2 3 4 5 6 7 8 9
struct DownloadLogFileNormalMIPS {
unsigned char command;
unsigned char subCommand;
LogIdentifier logId;
unsigned short entries_ago;
unsigned short entries_download;
unsigned char entries_skip;
DataSegmentMIPS flag;
} __attribute__((packed));
This way I get DownloadLogFileNormalMIPS as big as 10 bytes, where I was expecting 9. I'm doing something wrong? How to make DownloadLogFileNormalMIPS fit in 9 bytes?
May 7, 2014 at 9:29pm UTC
So you say sizeof(DownloadLogFileNormalMIPS) returns 10? Strange, for me it returns 9.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
enum LogIdentifier
{
LogIdentifierHouseKeeping = 0x01,
LogIdentifierMagnetometer = 0x02,
LogIdentifierDiffMIPS = 0x03,
LogIdentifierMIPS = 0x04,
LogIdentifierDiffSMDH = 0x05,
LogIdentifierSMDH = 0x06
} __attribute__((packed));
enum DataSegmentMIPS {
Counters_00_20 = 0x01,
Counters_21_50 = 0x02,
Counters_51_80 = 0x04
} __attribute__((packed));
struct DownloadLogFileNormalMIPS {
unsigned char command;
unsigned char subCommand;
LogIdentifier logId;
unsigned short entries_ago;
unsigned short entries_download;
unsigned char entries_skip;
DataSegmentMIPS flag;
} __attribute__((packed));
int main()
{
std::cout << sizeof (LogIdentifier) << std::endl; // 1
std::cout << sizeof (DataSegmentMIPS) << std::endl; // 1
std::cout << sizeof (DownloadLogFileNormalMIPS) << std::endl; // 9
}
May 7, 2014 at 9:30pm UTC
Can you change the order of the fields? It looks like you're getting a pad byte to force word alignment of the shorts. Move logId after entries_download.
May 7, 2014 at 9:38pm UTC
Peter87, I'm in a x64 win. Might that be the reason?
AbstractionAnon, Not possible to change the order.
Topic archived. No new replies allowed.