Author Topic: Size of a struct  (Read 268 times)

Uncle Buzz

  • Full Member
  • ***
  • Posts: 131
  • Country: fr
Size of a struct
« on: November 20, 2019, 03:51:17 AM »
Hi,
does someone have a trick to dynamically check the size of a struct before sending it on radio with memcpy orto write/read  in EEPROM or flash?

Maybe I'm cheated by my IDE (Visual Micro) but when I use sizeof(mystruct), as a struct is a collection of pointers, my IDE tells me sizeof(mystruct) is the size of the pointers collection and not the actual size of data which makes sens for me. Since a struct can contain byte, int, long, char... etc I don't know how to know the memory size of the data without defining a constant manually (which is a source of later problem)

I wonder if my IDE is reliable or if I should not believe it... in the code of EEPROMex library, to save a struct we can use the updateBlock function which use "sizeof" function to know how many bytes to write
Code: [Select]
	template <class T> int updateBlock(int address, const T& value)
{
int writeCount=0;
if (!isWriteOk(address+sizeof(value))) return 0;
const byte* bytePointer = (const byte*)(const void*)&value;
for (unsigned int i = 0; i < (unsigned int)sizeof(value); i++) {
if (read(address)!=*bytePointer) {
write(address, *bytePointer);
writeCount++;
}
address++;
bytePointer++;
}
return writeCount;
}

Uncle Buzz

  • Full Member
  • ***
  • Posts: 131
  • Country: fr
Re: Size of a struct
« Reply #1 on: November 20, 2019, 04:16:47 AM »
Reply to myself: sizeof is correctly returning the memory size of data and not only the size of pointers like my IDE says...

not sure if it's well translated, but in french we say "the right tools make the right workers"...