Arduino Forum

Using Arduino => Programming Questions => Topic started by: dede67 on Feb 01, 2017, 12:04 pm

Title: sizeof struct between ATmega328 and ESP8266
Post by: dede67 on Feb 01, 2017, 12:04 pm
Hi!
compiled for an ArduinoUno this struct has a size of 3 byte:
Code: [Select]
struct SensorData {
    signed int temp : 12;
  unsigned int ldr  : 10;
  unsigned int bad  : 1;
};


When compiled for ESP8266, it has a size of 4 byte. Even with this modification:
Code: [Select]
struct SensorData {
  int16_t  temp : 12;
  uint16_t ldr  : 10;
  uint8_t  bad  : 1;
};


What can I do, to have it three byte long on ESP8266 ?

Thanks in advance,
Detlev
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: AWOL on Feb 01, 2017, 12:09 pm
You could maybe try packing pragmas, but why is it important?
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: PaulS on Feb 01, 2017, 12:11 pm
Quote
What can I do, to have it three byte long on ESP8266 ?
Probably nothing. The bit count information is a suggestion to the compiler, not a requirement that it must implement.

Get rid of that, and live with a 4 byte struct on both platforms.
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: dede67 on Feb 01, 2017, 12:20 pm
It would result in 24*60=1440 unused byte in RAM and it also would need a more complex function for sending the entire array to a python-script (which expects a single sensordata in a length of three byte).
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: AWOL on Feb 01, 2017, 12:24 pm
So, do the packing yourself.
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: PaulS on Feb 01, 2017, 12:26 pm
Quote
It would result in 24*60=1440 unused byte in RAM
That implies that you are using more than 4320 bytes of SRAM on the Uno. How is THAT possible?
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: dede67 on Feb 01, 2017, 12:33 pm
That implies that you are using more than 4320 bytes of SRAM on the Uno. How is THAT possible?
On the Arduino, this data resides on two EEPROMs. 4KB in AT24C32 and 1KB inside of the ATmega328.
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: oqibidipo on Feb 01, 2017, 12:42 pm
Code: [Select]
struct SensorData { ... } __attribute__((packed));
 
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: dede67 on Feb 01, 2017, 01:00 pm
Code: [Select]
struct SensorData { ... } __attribute__((packed));
That works!
Thanks :-)
Title: Re: sizeof struct between ATmega328 and ESP8266
Post by: gfvalvo on Feb 01, 2017, 01:31 pm
I think packing will make access to your array of structures less efficient for the ESP8266. This is because members of some array elements will likely span 32-bit word boundaries. But, if memory use is more important in your application than processor cycles, then it should be OK.