Thread: Size of a struct

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    8

    Size of a struct

    Can you help me why this code gives 40 value of as the size of the struct
    Code:
    #include <stdio.h>
    
    
    struct clientData {
        unsigned int acctNum; // account number
        char lastName[15]; // account last name
        char firstName[10]; // account first name
        double balance; // account balance
    };
    
    
    int main () {
    	printf("%d", sizeof(struct clientData));
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    467
    Quote Originally Posted by Hetfield View Post
    Can you help me why this code gives 40 value of as the size of the struct
    Code:
    #include <stdio.h>
    
    
    struct clientData {
        unsigned int acctNum; // account number
        char lastName[15]; // account last name
        char firstName[10]; // account first name
        double balance; // account balance
    };
    
    
    int main () {
        printf("%d", sizeof(struct clientData));
    }

    structs are padded for alignment purposes. acctNum will be 4 bytes, lastName 16 (1 pad), firstname 12(2 pad) and balance 8 bytes.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    164
    Everything get padded to the nearest 4 bytes, depending on compiler alignment options and active "#pragma" directives. It might be like this:

    Code:
      struct clientData {
      unsigned int acctNum; // account number    4 bytes   no padding
      char lastName[15]; // account last name   15 bytes.  padded to 16
      char firstName[10]; // account first name  10 bytes, padded to 12
      double balance; // account balance          8 bytes, no padding
    };

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,039
    There's no reason to pad lastname to 16 since firstName, a char array, can start on any byte.
    It's more likely that firstName is padded to 13 so that balance is on an 8-byte boundary.
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    164
    Quote Originally Posted by john.c View Post
    There's no reason to pad lastname to 16 since firstName, a char array, can start on any byte.
    It's more likely that firstName is padded to 13 so that balance is on an 8-byte boundary.
    Yes, I can confirm that is of course true...

    Code:
    #include <stdio.h>
    #include <stddef.h>
    
    
    struct clientData {
      unsigned int acctNum; // account number    4 bytes   no padding
      char lastName[15]; // account last name   15 bytes.  padded to 16
      char firstName[10]; // account first name  10 bytes, padded to 12
      double balance; // account balance          8 bytes, no padding
    };
    
    
    int main(int argc, char *argv[]) {
       printf("acctNum   %zi\n", offsetof(struct clientData, acctNum));
       printf("lastName  %zi\n", offsetof(struct clientData, lastName));
       printf("firstName %zi\n", offsetof(struct clientData, firstName));
       printf("balance   %zi\n", offsetof(struct clientData, balance));
       return 0;
    }
    So here are the offsets in bytes from the start of the structure....

    Code:
    $ ./pad
    acctNum   0
    lastName  4
    firstName 19
    balance   32

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct field containing size of struct
    By DL1 in forum C Programming
    Replies: 7
    Last Post: 10-10-2011, 10:37 PM
  2. Struct array size
    By DavidDobson in forum C Programming
    Replies: 9
    Last Post: 08-19-2008, 11:13 AM
  3. Struct size incorrect
    By redneon in forum C Programming
    Replies: 21
    Last Post: 11-28-2007, 07:01 PM
  4. what is the size of following struct
    By vinayks in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 08:47 PM
  5. size of struct
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-06-2002, 10:56 AM

Tags for this Thread