Problem with sizeof ?

Hi All,

after a porting some code back to QNX4 I’m fiddle around with a curious
problem with the QNX6 2.92 compiler. Here the QNX6 code:

#include <stdio.h
#include <stdlib.h
#include <stddef.h
#include <unistd.h
#include <errno.h
#include <signal.h
#include <fcntl.h

#define BYTE unsigned char
#define TRANSMIT 1
#define EXTID 1


struct msg_conf
{
BYTE res : 2; // reserved
BYTE Xtd : 1; // extended frame flag
BYTE Dir : 1; // direction 1 = transmit
BYTE DLC : 4; // data length
};

struct can_usr_object
{
unsigned int ID;
struct msg_conf msgconf;
BYTE data[8];
};



int main(void)
{
int over_written = 0;
struct can_usr_object rmsg, msg;


printf(“sizeof struct can_usr_object: %d\n”, sizeof(struct can_usr_object));

printf(“offsetof struct can_usr_object, ID: %d\n”, offsetof(struct can_usr_object, ID));
printf(“offsetof struct can_usr_object, msgconf: %d\n”, offsetof(struct can_usr_object, msgconf));
printf(“offsetof struct can_usr_object, data: %d\n”, offsetof(struct can_usr_object, data));


}

COMPILE:

qcc -o x x.c

and that’s the output of x:

x

sizeof struct can_usr_object: 16

offsetof struct can_usr_object, ID: 0
offsetof struct can_usr_object, msgconf: 4
offsetof struct can_usr_object, data: 5

==> actual message length is 13 bytes and not 16.

Using sizeof(struct can-usr_object) in a memory move would
overwrite 3 byte in the memory.

Do I miss something??

Regards

–Armin





\

On Fri, Mar 31, 2006 at 06:28:07PM +0200, Armin Steinhoff wrote:

Using sizeof(struct can-usr_object) in a memory move would
overwrite 3 byte in the memory.

Do I miss something??

Padding is being added to your structure. There is a good explanation
here:

http://www.delorie.com/djgpp/v2faq/faq22_11.html


Regards,
Gilles

Hi Gilles,

many thanks for the useful link.

What still confusing is the fact that the struct seems to be packed …
that means the access to its cmponents isn’t optimized by padding.

Padding is only be done at the end of the structure for a 32 bit
alignment … so there is simply only a waste of 3 bytes and no access
speedup :frowning:

Regards

–Armin



Gilles Roy wrote:

On Fri, Mar 31, 2006 at 06:28:07PM +0200, Armin Steinhoff wrote:

Using sizeof(struct can-usr_object) in a memory move would
overwrite 3 byte in the memory.

Do I miss something??



Padding is being added to your structure. There is a good explanation
here:

http://www.delorie.com/djgpp/v2faq/faq22_11.html


Regards,
Gilles

Armin Steinhoff <a-steinhoff@web.de> wrote:

Hi Gilles,

many thanks for the useful link.

What still confusing is the fact that the struct seems to be packed …
that means the access to its cmponents isn’t optimized by padding.

Padding is only be done at the end of the structure for a 32 bit
alignment … so there is simply only a waste of 3 bytes and no access
speedup > :frowning:

Regards

–Armin

Consider if you declared an array of them. Without the padding
the int of the second member (for example) wouldn’t be aligned.
This would lead to a SIGBUS on some architectures.

Regards,

-seanb