SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct 
Author Message
 SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct

Okay, I understand that sizeof() is known for overestimating the size of
structures, but why is it underreporting this one..?

typedef struct {
  unsigned char a[4];
  unsigned char b : 1;
  unsigned char c : 1;
  unsigned char d : 1;
  unsigned char e : 1;
  unsigned char f : 1;
  unsigned char g : 1;
  unsigned char h : 2;
  union ua {
    unsigned char i
    unsigned char j;      
  };
  struct sb {
    unsigned char k;
    unsigned short l;
    unsigned short m;
  };

Quote:
} sa;

By my count, that's 11 bytes, right?  Well, sizeof() reports 5, ignoring
the union, and the nested structure.  Is that just a property of sizeof?
How do I output a variable of this type to a file without a correct
report from sizeof()?  Any help would be appreciated.. thanks in
advance.

rbright



Tue, 02 May 2000 03:00:00 GMT  
 SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct



...

Quote:
>Your count is wrong.  The struct you have declared does not contain
>any union or nested structure.  You've defined union ua and struct sb,
>but have not defined any instances of them.

This is actually a syntax error so requires a diagnostic from a
conforming compiler.

--
-----------------------------------------


-----------------------------------------



Wed, 03 May 2000 03:00:00 GMT  
 SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct

Quote:

> Okay, I understand that sizeof() is known for overestimating the size of
> structures, but why is it underreporting this one..?

The sizeof operator tells you exactly how much memory your object takes
up. Many programmers underestimate the size of their objects, and prefer
blaming the compiler to taking the time to understand what sizeof is
telling them. Don't fall for it. If sizeof tells you something different
from what you expect, you are almost certainly wrong. If the map doesn't
match the terrain, trust the terrain.
        -- Pete


Wed, 03 May 2000 03:00:00 GMT  
 SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct



Quote:
>Okay, I understand that sizeof() is known for overestimating the size of
>structures, but why is it underreporting this one..?

>typedef struct {
>  unsigned char a[4];
>  unsigned char b : 1;
>  unsigned char c : 1;
>  unsigned char d : 1;
>  unsigned char e : 1;
>  unsigned char f : 1;
>  unsigned char g : 1;
>  unsigned char h : 2;
>  union ua {
>    unsigned char i
>    unsigned char j;      
>  };
>  struct sb {
>    unsigned char k;
>    unsigned short l;
>    unsigned short m;
>  };
>} sa;

>By my count, that's 11 bytes, right?  Well, sizeof() reports 5, ignoring
>the union, and the nested structure.  Is that just a property of sizeof?
>How do I output a variable of this type to a file without a correct
>report from sizeof()?  Any help would be appreciated.. thanks in
>advance.

Your count is wrong.  The struct you have declared does not contain
any union or nested structure.  You've defined union ua and struct sb,
but have not defined any instances of them.

By the way, if sizeof overestimates the size of structures, the
implementation is broken.  sizeof should always give the exact size of
any object or type -- this may not be what you calculate if you don't
know the rules for the implementation you are targetting.

Michael M Rubenstein



Wed, 03 May 2000 03:00:00 GMT  
 SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct

Quote:

> Okay, I understand that sizeof() is known for overestimating the
> size of structures, but why is it underreporting this one..?

> typedef struct {
>   unsigned char a[4];
>   unsigned char b : 1;

7 x error: bit field must have type 'int', 'signed int', or
           'unsigned int'

Quote:
>   unsigned char c : 1;
>   unsigned char d : 1;
>   unsigned char e : 1;
>   unsigned char f : 1;
>   unsigned char g : 1;
>   unsigned char h : 2;
>   union ua {
>     unsigned char i

error: missing ';' before 'type'

Quote:
>     unsigned char j;
>   };

error: illegal declaration of anonymous 'union'

Quote:
>   struct sb {
>     unsigned char k;
>     unsigned short l;
>     unsigned short m;
>   };

error: illegal declaration of anonymous 'struct'

Quote:
> } sa;

> By my count, that's 11 bytes, right?  Well, sizeof() reports 5,
> ignoring the union, and the nested structure.  Is that just a
> property of sizeof?
> How do I output a variable of this type to a file without a
> correct report from sizeof()?  Any help would be appreciated..
> thanks in advance.

See if this works out better:

typedef struct {
  unsigned char a[4];
  unsigned int b : 1;
  unsigned int c : 1;
  unsigned int d : 1;
  unsigned int e : 1;
  unsigned int f : 1;
  unsigned int g : 1;
  unsigned int h : 2;
  union ua {
    unsigned char i;
    unsigned char j;      
  } x;
  struct sb {
    unsigned char k;
    unsigned short l;
    unsigned short m;
  } y;

Quote:
} sa;



Wed, 03 May 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. sizeof (struct) ! = sizeof struct elements

2. Sizeof(bitfield struct) == something strange - why?

3. sizeof a union that includes a struct

4. struct/union and sizeof (help)

5. native dll calling with structs containing arrays of nested structs

6. bitfields vs structs

7. bitfields in struct

8. Efficiency of using bitfields vs. structs

9. Question with struct/union

10. union or struct

11. unnamed struct/union in C99

12. C Parser to find Struct/Union Declaration

 

 
Powered by phpBB® Forum Software