sizeof() bug?
Posted: Fri Nov 14, 2014 8:21 pm
Looks like sizeof() reports the wrong size for the Union below.
Because I need to transfer the content by copying this buffer by looping through byte[0] .. byte[ sizeof(I2Cbuf) -1 ] I wanted to check if the union is setup correctly starting by looking at it's size using the code below.
The results are not what I expected to be and it looks like sizeof() is not reporting consistent values here..
I would think the size of I2Cbuf.flds should be 18+7= 25 and not 36..
I'm missing something here..?
Using v6.1.1
Code: Select all
union {
unsigned char byte[0];
struct {
struct {
unsigned char cmd; // 1 byte
float amount; // 4
float speed; // 4
float accel; // 4
float pos; // 4
unsigned char dir; // 1
} exec;
struct {
unsigned char lastCmd; // 1 byte
unsigned char lastExecStat; // 1 byte
float currPos; // 4 bytes
struct {
unsigned char fwd :1;
unsigned char moving :1;
unsigned char idle :1;
unsigned char sleep :1;
unsigned char pwrSave :1;
} bits; // 1 byte
} stat;
} flds;
} I2Cbuf;
The results are not what I expected to be and it looks like sizeof() is not reporting consistent values here..
Code: Select all
i = sizeof( I2Cbuf.flds );
// i -> 36bytes which would be the size of I2Cbuf......
i= 0;
i += sizeof( I2Cbuf.flds.exec.cmd );
i += sizeof( I2Cbuf.flds.exec.amount );
i += sizeof( I2Cbuf.flds.exec.speed );
i += sizeof( I2Cbuf.flds.exec.accel );
i += sizeof( I2Cbuf.flds.exec.pos );
i += sizeof( I2Cbuf.flds.exec.dir );
// i -> 18 which is correct IMHO and should be similar to I2Cbuf.flds.exec
i = sizeof( I2Cbuf.flds.exec );
// i-> 24.....!!!!!!
i= 0;
i += sizeof( I2Cbuf.flds.stat.bits );
i += sizeof( I2Cbuf.flds.stat.lastCmd );
i += sizeof( I2Cbuf.flds.stat.lastExecStat );
i += sizeof( I2Cbuf.flds.stat.currPos );
//i -> 7 which is correct IMHO
however:
i = sizeof( I2Cbuf.flds.stat );
//i -> 12 !!!!!!!!
I'm missing something here..?
Using v6.1.1