D
dashley
I have the following constructs:
typedef struct
{
unsigned char a[3];
unsigned short b; //Presumed 16 bits
unsigned char crc;
} mystruct;
mystruct x;
....
x.crc = CalculateCrc8(&x, sizeof(x) - 1);
//Designed to exclude CRC from CRC calculation.
I'm not sure if the compiler is C90 or C99.
Questions:
a)I'm assuming that the compiler is free to insert a padding byte after x.a?
b)I'm assuming that if you obtain the memory for the structure as an automatic or via malloc(), the padding byte could have any value, and only a block memory operation such as memset() set it to 0?
c)Is the CRC calculation call -- especially the use of "sizeof() - 1" in this way -- valid on all platforms? Specifically, can the compiler insert structure padding bytes AFTER x.crc so that subtracting 1 from sizeof() won'thave the desired effect of excluding the CRC itself from the CRC calculation?
Thanks for any information, especially about what is guaranteed behavior and not guaranteed behavior.
typedef struct
{
unsigned char a[3];
unsigned short b; //Presumed 16 bits
unsigned char crc;
} mystruct;
mystruct x;
....
x.crc = CalculateCrc8(&x, sizeof(x) - 1);
//Designed to exclude CRC from CRC calculation.
I'm not sure if the compiler is C90 or C99.
Questions:
a)I'm assuming that the compiler is free to insert a padding byte after x.a?
b)I'm assuming that if you obtain the memory for the structure as an automatic or via malloc(), the padding byte could have any value, and only a block memory operation such as memset() set it to 0?
c)Is the CRC calculation call -- especially the use of "sizeof() - 1" in this way -- valid on all platforms? Specifically, can the compiler insert structure padding bytes AFTER x.crc so that subtracting 1 from sizeof() won'thave the desired effect of excluding the CRC itself from the CRC calculation?
Thanks for any information, especially about what is guaranteed behavior and not guaranteed behavior.