NO

Author Topic: why its size is 11 not 9?  (Read 4390 times)

Xsoda

  • Guest
why its size is 11 not 9?
« on: August 06, 2012, 08:34:09 AM »
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?

Code: [Select]
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()

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: why its size is 11 not 9?
« Reply #1 on: August 06, 2012, 12:32:06 PM »
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.

Code: [Select]
#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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: why its size is 11 not 9?
« Reply #2 on: August 06, 2012, 08:53:53 PM »
Makes me wonder if that is related to the strange memove/memset issues?  ???

Ralf

CommonTater

  • Guest
Re: why its size is 11 not 9?
« Reply #3 on: August 06, 2012, 09:40:37 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. 
 

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: why its size is 11 not 9?
« Reply #4 on: August 07, 2012, 01:09:44 AM »
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

  • Guest
Re: why its size is 11 not 9?
« Reply #5 on: August 07, 2012, 01:35:47 AM »
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  :'( 
 
 

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: why its size is 11 not 9?
« Reply #6 on: September 01, 2012, 08:59:29 PM »
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