sizeof(struct) returns a SMALLER than expected value?

nass

I have this c++ structure:

struct Packet
{
    uint32 MessageCount; 
    uint32 Length; 
    uint32 FieldValue;

    union PacketHeader
    {
        uint32 typeInfo; 
        struct MagicVersion
        {
            uint8 MagicNumber[3];
            uint8 Version; 
        };
     };

    Data * Payload(void) { return reinterpret_cast< Data * >(this + 1U); }
    Data const * Payload(void) const { return reinterpret_cast< Data const * >(this + 1U); }

    Packet * nextPacket(void) { return reinterpret_cast< Packet * >(this + 1U) + Length; }
    Packet const * nextPacket(void) const { return reinterpret_cast< Packet const * >(this + 1U) + Length; }
};

Then sizeof(Packet) in MSVC++ returns 12 instead of 16 which is what I expect.

The weird thing is of course that this is smaller that the expected value. Had it been bigger it could be because of alignment issues.

What am I missing?

TIA

Dutow

Why do you think it should be bigger?

It contains 3 uint32 with no virtual methods, that's exactly 12 bytes.

The union doesn't count, since it's a type, it contains no member of that type.

If you want your class to contain a single instance of the union, which contains a single instance of the struct, you should write:

struct Packet
{
    uint32 MessageCount; 
    uint32 Length; 
    uint32 FieldValue;

    union
    {
        uint32 typeInfo; 
        struct
        {
            uint8 MagicNumber[3];
            uint8 Version; 
        } /* MagicVersion */;
     };
} /* PacketHeader */;

The names within /**/ are optional, you can either specify them or not. If you do, you have to access their members using the name of the union/struct, otherwise you'll have a flat Packet struct.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Are all the values on the Stack smaller than a passed value?

From Dev

Android ListView item getMeasuredHeight() returns a smaller value than the real one

From Dev

Function returns nothing rather than the expected value "None"

From Dev

Max value from list that is smaller than X

From Dev

Buffer length smaller than expected

From Dev

Ensure float to be smaller than exact value

From Dev

UIScreen bounds smaller than expected on external screen

From Dev

matlab spectrum returns more FRAME than expected

From Dev

Scrapy returns more results than expected

From Dev

MySQL Join returns more than expected

From Dev

Image in ImageView showing up smaller than expected (Android)

From Dev

Clonezilla image size smaller than expected

From Dev

ELF header smaller than expected

From Dev

Clonezilla image size smaller than expected

From Dev

Are all the values on the Stack smaller than a passed value?

From Dev

Android ListView item getMeasuredHeight() returns a smaller value than the real one

From Dev

Can't set the resolution (resolution smaller than expected)

From Dev

Error in C program smaller than expected

From Dev

Curl: Returns weird output than expected, but why?

From Dev

Function returns nothing rather than the expected value "None"

From Dev

blkid returns more information than expected

From Dev

Buffer length smaller than expected

From Dev

PHP array returns different value than expected

From Dev

children().count() of QGroupBox returns value more than expected

From Dev

QuerySelectorAll returns more than expected

From Dev

MySQL Join returns more than expected

From Dev

Why is my RAID5 array smaller than expected?

From Dev

Maximal subset sum smaller than a given value

From Dev

Length of character array after fread is smaller than expected

Related Related

  1. 1

    Are all the values on the Stack smaller than a passed value?

  2. 2

    Android ListView item getMeasuredHeight() returns a smaller value than the real one

  3. 3

    Function returns nothing rather than the expected value "None"

  4. 4

    Max value from list that is smaller than X

  5. 5

    Buffer length smaller than expected

  6. 6

    Ensure float to be smaller than exact value

  7. 7

    UIScreen bounds smaller than expected on external screen

  8. 8

    matlab spectrum returns more FRAME than expected

  9. 9

    Scrapy returns more results than expected

  10. 10

    MySQL Join returns more than expected

  11. 11

    Image in ImageView showing up smaller than expected (Android)

  12. 12

    Clonezilla image size smaller than expected

  13. 13

    ELF header smaller than expected

  14. 14

    Clonezilla image size smaller than expected

  15. 15

    Are all the values on the Stack smaller than a passed value?

  16. 16

    Android ListView item getMeasuredHeight() returns a smaller value than the real one

  17. 17

    Can't set the resolution (resolution smaller than expected)

  18. 18

    Error in C program smaller than expected

  19. 19

    Curl: Returns weird output than expected, but why?

  20. 20

    Function returns nothing rather than the expected value "None"

  21. 21

    blkid returns more information than expected

  22. 22

    Buffer length smaller than expected

  23. 23

    PHP array returns different value than expected

  24. 24

    children().count() of QGroupBox returns value more than expected

  25. 25

    QuerySelectorAll returns more than expected

  26. 26

    MySQL Join returns more than expected

  27. 27

    Why is my RAID5 array smaller than expected?

  28. 28

    Maximal subset sum smaller than a given value

  29. 29

    Length of character array after fread is smaller than expected

HotTag

Archive