Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Cast byte array to struct

Tags:

c++

arrays

c

struct

I have the problem of casting a byte array to a struct, some bytes are ignored or skipped.

Given the following struct,

typedef struct
{
    uint32_t id;
    uint16_t test;
    uint8_t group;
    uint32_t time;
    uint16_t duration;
    uint8_t a;
    uint8_t b;
    uint8_t c;
    uint16_t d;
    uint16_t e;
    uint8_t status;
    uint8_t x;
    uint8_t y;

} testStruct_t, *PtestStruct_t;

I have an array with the following test data:

uint8_t pBuff = { 0x11 , 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19 };

The casting is done as follows:

PtestStruct_t pStruct = (PtestStruct_t)pBuff;

Somewhere in the structure some bytes are skipped or ignored. I do not know why. This has been tested in Visual Studio 2012 and on a ARM processor on which this testing and debugging was made required.

What am I missing here? I do not believe it is Endian related. It could be the compiler in both test cases, I do not know what to do in this last case.

The bytes which are being skipped/ignored are 0x88 and 0x14

like image 694
Armandt Avatar asked Nov 15 '13 07:11

Armandt


2 Answers

You're encountering alignment padding.

uint32_t id;     // offset 0
uint16_t test;   // offset 4
uint8_t group;   // offset 6
uint32_t time;   // offset 7

The offsets shown here are likely to be wrong. The compiler will probably place padding between "group" and "time" to ensure that "time" is on a 4-byte boundary (the actual alignment is configurable)

If you absolutely require the structure to be like that, you can use #pragma pack

#pragma pack(push, 1)
typedef struct
{
    uint32_t id;
    uint16_t test;
    uint8_t group;
    uint32_t time;
    uint16_t duration;
    uint8_t a;
    uint8_t b;
    uint8_t c;
    uint16_t d;
    uint16_t e;
    uint8_t status;
    uint8_t x;
    uint8_t y;

} testStruct_t, *PtestStruct_t;
#pragma pack(pop)
like image 172
kfsone Avatar answered Oct 15 '22 07:10

kfsone


Compiler may have added some byte between your struct fields for alignment. You need to use a packing to prevents compiler from doing padding - this has to be explicitly requested - under GCC it's attribute((packed)),

example:

      struct __attribute__((__packed__)) mystruct_A {
      char a;
      int b;
     char c;
     };

and for Visual Studio consult MSDN

like image 25
alexbuisson Avatar answered Oct 15 '22 07:10

alexbuisson