packing of structs in memory

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

Im converting come code from the AT90USB1287 to a samd21, everyting was going smoothly then I ran across this issue with an array of structs, the struct has the following definition:

typedef struct __attribute__((packed,aligned(1)))
{
	U16	Type;				// 2 bytes offset 0 what type of event is this, defined
	union									// max union 4 bytes
	{
		unsigned long		GMT_Time;		// 4 bytes
		unsigned long		Time;
		unsigned long		Id;
	}a;
	union									// max 4 bytes
	{
		unsigned long		CFG_Date;
		unsigned long		Place;
		U16	Rssi;
		U16	Position;
	}b;
	union									// max 6 bytes
	{
		unsigned long		APP_Date;		// 4
		unsigned char 		Ibutton[6];		// 6
		STATUS		Status;					// 4 bytes
		U16	Period;			// 2
		U16	Param[3];		// 6
		unsigned long		Date;			//4
	}c;
}EVT;

and is declared :

EVT			RX_Buff[4];

The buffer is loaded form a data modem, the problem is that when i look at the actual struct data it seems to be offset on an 18byte boundary

the actual data is stored like this:

0x20001C34  2c 00 ad 8a f6 5b 06 14 10 00 00 00 00 00 00 00
0x20001C44  35 00 74 96 f6 5b 06 14 10 00 00 00 12 b9 56 9e
0x20001C54  36 00 b5 96 f6 5b 06 14 10 00 00 00 12 b9 56 9e
0x20001C64  35 00 62 98 f6 5b 06 14 10 00 00 00 12 b9 56 9e

but the struct reads as in the attached image. I have tried using the pack and aligned attributes, but it doesn't seem to make any difference. can anyone help?

Thanks

Rob Carter

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try this:

 

typedef struct __attribute__((packed,aligned(1)))
{
	u16	Type;				// 2 bytes offset 0 what type of event is this, defined
	union									// max union 4 bytes
	{
		unsigned long		GMT_Time;		// 4 bytes
		unsigned long		Time;
		unsigned long		Id;
	}a;
	union									// max 4 bytes
	{
		unsigned long		CFG_Date;
		unsigned long		Place;
		u16	Rssi;
		u16	Position;
	}b;
	union __attribute__((packed,aligned(1)))									// max 6 bytes
	{
		unsigned long		APP_Date;		// 4
		unsigned char 		Ibutton[6];		// 6
		STATUS		Status;					// 4 bytes
		u16	Period;			// 2
		u16	Param[3];		// 6
		unsigned long		Date;			//4
	}c;
}EVT;

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That did the trick.

Thanks for your help

Rob