How to find the size of structure?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnagJohari
    New Member
    • May 2010
    • 96

    How to find the size of structure?

    hello guys, Can u tell me how to find the size of structure.
    Please explain by giving some example.
    Thank you
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    Add the size of your structure's members and you will have the structure's size!

    Code:
    typedef struct
    {
      int a;
      float b;
    } MY_STRUCT;

    The size of MY_STRUCT would be sizeof(int) + sizeof(float).
    So you can use the sizeof() function to acquire this information during runtime.

    Code:
    unsigned char MY_STRUCTSize = sizeof(MY_STRUCT);

    And if you want to verify that the structure is the sum of its members:

    Code:
    MY_STRUCT myStruct;
    unsigned char myStructSize = sizeof(myStruct);
    unsigned char myStructSizea = sizeof(myStruct.a);
    unsigned char myStructSizeb = sizeof(myStruct.b);

    Note that I am not giving you fixed sizes because they depend on the platform and compiler. For my embedded enviroment's compiler, variables sizes are platform independent. So if I compile the above code I will get sizeof(myStruct .a) = 2, sizeof(myStruct .b) = 4 and sizeof(myStruct ) = 6.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Add the size of your structure's members and you will have the structure's size!

      The size of MY_STRUCT would be sizeof(int) + sizeof(float).
      No generally this is not true.

      Code:
      MY_STRUCT myStruct;
      unsigned char myStructSize = sizeof(myStruct);
      unsigned char myStructSizea = sizeof(myStruct.a);
      unsigned char myStructSizeb = sizeof(myStruct.b);
      If you do this you need to be very careful because on the majority of platforms

      myStructSize != myStructSizea + myStructSizeb

      because the compiler puts padding bytes into the structure to maintain correct member alignment for efficient access.

      sizeof(myStruct ) will give you the size of the structure including any padding bytes, for example should you want to use malloc to allocate memory for it.


      The are some platforms, typically 8bit ones where the compiler does not include any padding bytes on which the size of the structure is the sum of the size of its members.

      Comment

      • AnagJohari
        New Member
        • May 2010
        • 96

        #4
        Originally posted by Banfa
        No generally this is not true.

        Code:
        MY_STRUCT myStruct;
        unsigned char myStructSize = sizeof(myStruct);
        unsigned char myStructSizea = sizeof(myStruct.a);
        unsigned char myStructSizeb = sizeof(myStruct.b);
        If you do this you need to be very careful because on the majority of platforms

        myStructSize != myStructSizea + myStructSizeb

        because the compiler puts padding bytes into the structure to maintain correct member alignment for efficient access.

        sizeof(myStruct ) will give you the size of the structure including any padding bytes, for example should you want to use malloc to allocate memory for it.


        The are some platforms, typically 8bit ones where the compiler does not include any padding bytes on which the size of the structure is the sum of the size of its members.
        can u give the example in which padding is used so can know what is the size of structure.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          The example already given could have padding. Like I said most structures could. Typically padding occurs when you have a structure with several differently sized objects in it for example

          Code:
          struct example1
          {
            char a;
            short b;
            float c;
          };
          
          struct example2
          {
            float c;
            short b;
            char a;
          };
          How much padding a structure has is entirely platform dependent.

          In this case sizeof(example1 ) will always return the size of the structure on the current platform, including padding bytes.

          Comment

          • AnagJohari
            New Member
            • May 2010
            • 96

            #6
            Originally posted by Banfa
            The example already given could have padding. Like I said most structures could. Typically padding occurs when you have a structure with several differently sized objects in it for example

            Code:
            struct example1
            {
              char a;
              short b;
              float c;
            };
            
            struct example2
            {
              float c;
              short b;
              char a;
            };
            How much padding a structure has is entirely platform dependent.

            In this case sizeof(example1 ) will always return the size of the structure on the current platform, including padding bytes.
            if we declare two integer varibles then what is the size of structure.
            like this
            Code:
            struct st1
            {
            int a;
            int b;
            }
            in my opinion the ans is 4
            but the actual ans is 2
            i donot know what is the reason behind this

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              When i run this code in my Linux machine (gcc version 3.4.6 20060404). The output is
              Code:
              8
              This is as expected(since int is taking 4 bytes.)

              If your machine is taking 2 bytes to represent an int then the size of the structure will be 4.

              Regards
              Dheeraj Joshi

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by AnagJohari
                if we declare two integer varibles then what is the size of structure.
                like this
                Code:
                struct st1
                {
                int a;
                int b;
                }
                in my opinion the ans is 4
                but the actual ans is 2
                i donot know what is the reason behind this
                in my opinion the ans is 4
                but the actual ans is 2
                i donot know what is the reason behind this
                neither do we/I unless you show us the code you used to get the "actual answer".

                Comment

                Working...