News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

why its size is 11 not 9?

Started by Xsoda, August 06, 2012, 08:34:09 AM

Previous topic - Next topic

Xsoda

Hello, everyone.
I write a struct, and i think its sizeof() test should is 9 not 11. but i use pelles c 7.0 execute it, tell me it's 11. in gcc , it is 9? can you resolve this puzzle?


typedef unsigned char __u8;
typedef unsigned short __u16;
typedef unsigned int __u32;

#pragma pack(1)
typedef struct  packet_prefix {
    __u8 start;
    __u8 len;
    struct {
        __u8 commMode:6;
        __u8 DIR:1;
        __u8 PRM:1;
    } control;
    union {
        struct {
            __u8 routerID:1;
            __u8 affiliatedNodeID:1;
            __u8 commModuleID:1;
            __u8 collisionDetection:1;
            __u8 relayLevel:4;

            __u8 channelID:4;
            __u8 errorCorrectionCodeID:4;

            __u8 responseBytes;

            __u16 commBaudRate:15;
            __u16 rateUnitID:1;

            __u8 reserve;
        } info_down;

        struct {
            __u8 routerID:1;
            __u8 no_use1:1;
            __u8 commModuleID:1;
            __u8 no_use2:1;
            __u8 relayLevel:4;

            __u8 channelID:4;
            __u8 no_use3:4;

            __u8 phaseLineMark:4;
            __u8 meterChannelFeature:4;

            __u8 lastCommandSignalQuality:4;
            __u8 lastResponseSignalQuality:4;
            __u16 reserve;
        } info_up;
    }info;
} packet_prefix;
#pragma pack()

frankie

It's a bug.
The compiler aligns anyway the double byte bitfields on WORD boundary despite the #pragma pack.
If you move the the fields in the structures you get the right size that is 9 bytes.

#pragma pack(1)
typedef struct  __packet_prefix
{
    __u8 start;
    __u8 len;
    struct
{
        __u8 commMode:6;
        __u8 DIR:1;
        __u8 PRM:1;
    } control;
    union
{
        struct _info_down
{
            __u8 routerID:1;
            __u8 affiliatedNodeID:1;
            __u8 commModuleID:1;
            __u8 collisionDetection:1;
            __u8 relayLevel:4;

            __u8 channelID:4;
            __u8 errorCorrectionCodeID:4;

            __u8 responseBytes;

            __u8 reserve;                //If you move the last here, the next bitfield is on WORD boundary and packing works
            __u16 commBaudRate:15;
            __u16 rateUnitID:1;
//            __u8 reserve;
        } info_down;

        struct _info_up
{
            __u8 routerID:1;
            __u8 no_use1:1;
            __u8 commModuleID:1;
            __u8 no_use2:1;
            __u8 relayLevel:4;

            __u8 channelID:4;
            __u8 no_use3:4;

            __u8 phaseLineMark:4;
            __u8 meterChannelFeature:4;

//            __u16 reserve:16;     //if you uncomment this and comment the next the bug appears anyway
            __u8 lastCommandSignalQuality:4;
            __u8 lastResponseSignalQuality:4;
            __u16 reserve;      //not a bitfield packing works
        } info_up;
    }info;
} packet_prefix;
#pragma pack()
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Bitbeisser

Makes me wonder if that is related to the strange memove/memset issues?  ???

Ralf

CommonTater

Quote from: Bitbeisser on August 06, 2012, 08:53:53 PM
Makes me wonder if that is related to the strange memove/memset issues?  ???
Ralf

Hey Ralf  :)

Akko should probably test his original algorythms with Optimizations off.
It's starting to look like there are quite a few problems there. 

Bitbeisser

Quote from: CommonTater on August 06, 2012, 09:40:37 PM
Quote from: Bitbeisser on August 06, 2012, 08:53:53 PM
Makes me wonder if that is related to the strange memove/memset issues?  ???
Ralf

Hey Ralf  :)

Akko should probably test his original algorythms with Optimizations off.
It's starting to look like there are quite a few problems there.
As I clearly stated, I did test it with both optimization on and off, with no difference to the problem.

Just the way how the variables was declared made a difference and I did not get around yet to check closer into what is going on. Just strikes me odd that the "one off" memmove/memset issue seems to kind of fit the packing/alignment mentioned here...

Ralf

CommonTater

Quote
As I clearly stated, I did test it with both optimization on and off, with no difference to the problem.

My bad... faulty memory... should have checked first.  Sorry  :'( 


Pelle

Quote from: Xsoda on August 06, 2012, 08:34:09 AM
I write a struct, and i think its sizeof() test should is 9 not 11. but i use pelles c 7.0 execute it, tell me it's 11. in gcc , it is 9? can you resolve this puzzle?
OK, I will look at this...
/Pelle