Like Tree2Likes
  • 1 Post By Cat
  • 1 Post By grumpy

Puzzled about the size of my data structure

This is a discussion on Puzzled about the size of my data structure within the C Programming forums, part of the General Programming Boards category; Hi, I have created the following data type... Code: typedef struct tagLFSRType { union { struct { unsigned int bit0 ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Albuquerque, NM USA
    Posts
    5

    Puzzled about the size of my data structure

    Hi, I have created the following data type...
    Code:
    typedef struct tagLFSRType {
       union {
          struct {
             unsigned int bit0 : 1; // 
             unsigned int bit1 : 1; // 
             unsigned int bit2 : 1; // 
             unsigned int bit3 : 1; // 
             unsigned int bit4 : 1; // 
             unsigned int bit5 : 1; // 
             unsigned int bit6 : 1; // 
             unsigned int bit7 : 1; // 
             unsigned int bit8 : 1; // 
             unsigned int bit9 : 1; // 
             unsigned int bit10 : 1; // 
             unsigned int bit11 : 1; // 
             unsigned int bit12 : 1; // 
             unsigned int bit13 : 1; // 
             unsigned int bit14 : 1; // 
             unsigned int bit15 : 1; // 
             unsigned int bit16 : 1; // 
             unsigned int bit17 : 1; // 
             unsigned int bit18 : 1; // 
             unsigned int bit19 : 1; // 
             unsigned int bit20 : 1; // 
             unsigned int bit21 : 1; // 
             unsigned int bit22 : 1; // 
             unsigned int bit23 : 1; // 
             unsigned int bit24 : 1; // 
             unsigned int bit25 : 1; // 
             unsigned int bit26 : 1; // 
             unsigned int bit27 : 1; // 
             unsigned int bit28 : 1; // 
             unsigned int bit29 : 1; // 
             unsigned int bit30 : 1; // 
             unsigned int bit31 : 1; // 
          } b;               
          unsigned int value;
       } BitField;
       unsigned char length;
    } LFSR_type;
    When I print the sizeof(LFSR_type), I get 8. I would have expected to get 5. Also, when I create an array of them and print the addresses of them, the address increment by 8 as well.
    Is there an obvious explanation for this?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Have you considered just using an unsigned 32 bit integer and just deleting this structure?

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Albuquerque, NM USA
    Posts
    5
    I would like to be able to access each of the bit values as a field.

    Actually, this works well.
    Code:
    #define BIT(val, bit) ((val & (1 << bit)) >> bit)
    
    int main(int argc, char *argv[])
    {
       unsigned int value;
       int i;
    
       if (argc == 2) {
          sscanf(argv[1], "%8X", &value);
       } else {
          printf("Usage: %s <hex_value>\n", argv[0]);
          exit(EXIT_FAILURE);
       }
    
       for (i = 31; i >= 0; i--) {
          printf("%d", BIT(value, i));
          if ((i%4) == 0) printf(" ");
       }
       printf("\n");
    
       exit(EXIT_SUCCESS);
    }
    But thanks for your input.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    252
    The difference between expected and actual size is
    because with structures the memory assigned is optimised
    for the computer/compiler. This means a single byte may get
    padded out to 4 bytes, depending on how it's aligned with the
    rest of the structure variables, and also the optimisation the
    compiler uses.Different compilers may use different optimisations,
    so using sizeof() on the same structure on different systems may
    provide different results.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,246
    Actually, this works well.
    Are you aware that you are shifting back and forth?

    I'd put a little more thought into that if I were you.

    *shrug*

    And anyways, this actually works better; "bit fields" can cause some problems especially when portability comes up.

    Soma

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by jaarestad View Post
    When I print the sizeof(LFSR_type), I get 8. I would have expected to get 5. Also, when I create an array of them and print the addresses of them, the address increment by 8 as well.
    Is there an obvious explanation for this?
    In general, the size of an array or struct is *at least* as large as the sum of all constituent members' sizes, but it could well be larger. Padding and data alignment can vary platform to platform and sometimes compiler to compiler. For example, it's not uncommon for hardware to require that a 4-byte read is aligned to a 4-byte boundary (i.e. you could read 32 bits starting from addresses ending in 0, 4, 8, or C in hex, but not starting from any other address). This is a consequence of how the hardware is built - it's faster, consumes less power, and takes fewer transistors to make hardware that only does aligned reads.

    Software of course could get around this by doing multiple aligned reads and doing bitwise manipulation of the results, but it's usually better performance just to keep things aligned. In the case of a 4-byte value in a structure, the struct in this case would be padded such that the 4-byte value is always aligned to a boundary.
    Salem likes this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    Another problem you will face is that you have no control over the order in which the compiler assigns bits in a bit field.

    You can say
    var.value = 1;

    But that doesn't mean you're guaranteed that var.b.bit0 is now 1, because the compiler could have assigned .bit0 to bit31 of the underlying storage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,284
    Quote Originally Posted by Cat View Post
    In general, the size of an array or struct is *at least* as large as the sum of all constituent members' sizes, but it could well be larger.
    This does not apply to arrays. The size of an array is always exactly equal to the number of elements multiplied by the size of an element.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of structure
    By Leone in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2010, 07:56 AM
  2. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  5. SIze of structure
    By Juganoo in forum C Programming
    Replies: 4
    Last Post: 12-03-2002, 09:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21