Thread: Size of structure

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    40

    Size of structure

    I wrote code like this
    Code:
    #include<stdio.h>#include<stdlib.h>
    struct Name{
    	char n;
    	char m;
    	struct Name *N;
    };
    void main (){
      struct Name *H = (struct Name*)malloc(sizeof (struct Name));
      H->n;
      H->m;
      H->N;
      
      printf("%d \n",  sizeof (H->n));
      printf("%d \n", sizeof (H->m));
      printf("%d \n", sizeof (H->N));
      printf("%d \n", sizeof (struct Name));
    }
    When I ran code
    It gives below output

    1
    1
    4
    8

    so I understand
    H->n; take one byte
    H->m; take one byte

    result say H->N; 4 bytes

    I do not understand why does it take 4 bytes ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That just means that a pointer to object type takes 4 bytes for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    5
    Quote Originally Posted by skyr6546 View Post
    I wrote code like this
    Code:
    #include<stdio.h>#include<stdlib.h>
    struct Name{
        char n;
        char m;
        struct Name *N;
    };
    void main (){
      struct Name *H = (struct Name*)malloc(sizeof (struct Name));
      H->n;
      H->m;
      H->N;
      
      printf("%d \n",  sizeof (H->n));
      printf("%d \n", sizeof (H->m));
      printf("%d \n", sizeof (H->N));
      printf("%d \n", sizeof (struct Name));
    }
    When I ran code
    It gives below output

    1
    1
    4
    8

    so I understand
    H->n; take one byte
    H->m; take one byte

    result say H->N; 4 bytes

    I do not understand why does it take 4 bytes ?
    The actual values you get will be system dependent. I cleaned you code up to get rid of the basic warnings it generates add ran it on my system.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct Name{
        char n;
        char m;
        struct Name *N;
    };
    
    int  main (){
      struct Name *H = malloc(sizeof (struct Name));
      if (H == NULL) {
        fprintf (stderr, "Malloc failed\n"); 
        exit(EXIT_FAILURE);
      }
      printf("%ld \n",  sizeof (H->n));
      printf("%ld \n", sizeof (H->m));
      printf("%ld \n", sizeof (H->N));
      printf("%ld \n", sizeof H);
      printf("%ld \n", sizeof *H);
      printf("%ld \n", sizeof (struct Name));
    
    
      free (H);
    
      return 0;
    }
    On the system I'm using compiled with:
    gcc -std=c11 -Wall size_struct.c -o size_struct

    I get:
    1
    1
    8
    8
    16
    16

    I'm using a 64 bit system so pointers are 8 bytes in size (8*8=64). My compiler has added padding to the struct to aid alignment. I used the specifier %ld because the return value of sizeof is a long int.

    For every malloc in your program you should call free - an you should get in the habit of checking the return value. It may seem like a lot of extra typing for an example but it's a habit worth cultivating.

    Worth noting is that the pointer N is never initialized so will not be valid and shouldn't be de-referenced until it is initialized.

    sizeof is actually an operator and when referring to an actual object you can call it without brackets.

    There may be things your compiler can tell you to help you learn C. Learning to turn the warnings up and then trying to understand them so you can clean up your code (that would be compiler dependent of course) can be very valuable.

    -Greg.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by G.Martin
    I used the specifier %ld because the return value of sizeof is a long int.
    That's not accurate. The type of the result of sizeof is size_t, and since you're compiling with respect to C11, the corresponding format specifier is %zu.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    40
    Structure size = (H->n) + (H->m) + (H->N)
    = 1 + 1 + 4
    = 6

    But compiler showing size is 8 bytes

    Am I wrong in my calculation? I do not understand why it show two extra bytes

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C structure size
    By CANcprogram in forum C Programming
    Replies: 2
    Last Post: 07-18-2017, 03:09 AM
  2. Puzzled about the size of my data structure
    By jaarestad in forum C Programming
    Replies: 7
    Last Post: 03-28-2013, 06:09 AM
  3. size of structure
    By Leone in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2010, 08:56 AM
  4. Size of structure in bytes
    By Micko in forum C Programming
    Replies: 2
    Last Post: 11-29-2004, 10:09 AM
  5. SIze of structure
    By Juganoo in forum C Programming
    Replies: 4
    Last Post: 12-03-2002, 10:23 PM

Tags for this Thread