Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to find out the size of a structure without using sizeof

How to find out the size of a structure without using sizeof

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rajasegar
    wrote on last edited by
    #1

    Can we find out the size of a structure without using sizeof function? I have tried to write a macro passing the name of the structure as a parameter, but it did not work as expected!! Is there anyother way to do this? Please provide your valuable suggestions and code(if possible).....

    R@j@$eg@r

    D J C B 4 Replies Last reply
    0
    • R Rajasegar

      Can we find out the size of a structure without using sizeof function? I have tried to write a macro passing the name of the structure as a parameter, but it did not work as expected!! Is there anyother way to do this? Please provide your valuable suggestions and code(if possible).....

      R@j@$eg@r

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Yes.


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      1 Reply Last reply
      0
      • R Rajasegar

        Can we find out the size of a structure without using sizeof function? I have tried to write a macro passing the name of the structure as a parameter, but it did not work as expected!! Is there anyother way to do this? Please provide your valuable suggestions and code(if possible).....

        R@j@$eg@r

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #3

        Homework-time, eh? Hint: You can get adresses of struct members...


        Failure is not an option - it's built right in.

        1 Reply Last reply
        0
        • R Rajasegar

          Can we find out the size of a structure without using sizeof function? I have tried to write a macro passing the name of the structure as a parameter, but it did not work as expected!! Is there anyother way to do this? Please provide your valuable suggestions and code(if possible).....

          R@j@$eg@r

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Even sizeof usage is hazardous on Win32 structures. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          D 1 Reply Last reply
          0
          • C CPallini

            Even sizeof usage is hazardous on Win32 structures. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Why?


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            R C 2 Replies Last reply
            0
            • D David Crow

              Why?


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              R Offline
              R Offline
              Rajasegar
              wrote on last edited by
              #6

              Since we are working on generating metadata about a structure by supplying the name of the structure, and getting its size during run time!!

              R@j@$eg@r

              D 1 Reply Last reply
              0
              • D David Crow

                Why?


                "A good athlete is the result of a good and worthy opponent." - David Crow

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Hint: BITMAPINFO :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                D 1 Reply Last reply
                0
                • C CPallini

                  Hint: BITMAPINFO :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Is its size undetectable?


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  C 1 Reply Last reply
                  0
                  • R Rajasegar

                    Since we are working on generating metadata about a structure by supplying the name of the structure, and getting its size during run time!!

                    R@j@$eg@r

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    This makes no sense. How does it answer the question, "Why is sizeof usage hazardous on Win32 structures?"


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    1 Reply Last reply
                    0
                    • D David Crow

                      Is its size undetectable?


                      "A good athlete is the result of a good and worthy opponent." - David Crow

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #10

                      Of course it is detectable. But its actual size cannot be detected with a naive use of sizeof, i.e. it's tricky!:-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                      E 1 Reply Last reply
                      0
                      • C CPallini

                        Of course it is detectable. But its actual size cannot be detected with a naive use of sizeof, i.e. it's tricky!:-D

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                        E Offline
                        E Offline
                        Emmanouil
                        wrote on last edited by
                        #11

                        I agree. sizeof will return different size for the same structure if executed in different platforms i.e linux vs windows and thus different compilers. This is due to padding and alignment of members for optimization purposes in the memory. There are macros or compiler options that can be used to eliminate padding i.e to use 1 byte padding always. If i remember correctly this will be the "pack(1)" macro and in Visual C++ the compiler option /Z1 (I think) can be used to force 1 byte padding. This will give more consistent results with sizeof... :-).

                        D 1 Reply Last reply
                        0
                        • E Emmanouil

                          I agree. sizeof will return different size for the same structure if executed in different platforms i.e linux vs windows and thus different compilers. This is due to padding and alignment of members for optimization purposes in the memory. There are macros or compiler options that can be used to eliminate padding i.e to use 1 byte padding always. If i remember correctly this will be the "pack(1)" macro and in Visual C++ the compiler option /Z1 (I think) can be used to force 1 byte padding. This will give more consistent results with sizeof... :-).

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          Emmanouil wrote:

                          I agree. sizeof will return different size for the same structure if executed in different platforms i.e linux vs windows and thus different compilers.

                          I'm not exactly sure what you are agreeing to, but, for example, sizeof(int) will also return different results on different platforms. That's not a fault with the sizeof operator, however.


                          "A good athlete is the result of a good and worthy opponent." - David Crow

                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                          1 Reply Last reply
                          0
                          • R Rajasegar

                            Can we find out the size of a structure without using sizeof function? I have tried to write a macro passing the name of the structure as a parameter, but it did not work as expected!! Is there anyother way to do this? Please provide your valuable suggestions and code(if possible).....

                            R@j@$eg@r

                            B Offline
                            B Offline
                            Bram van Kampen
                            wrote on last edited by
                            #13

                            There is some crap being posted here. The only time this is an issue is when you receive a pointer to a structure. If you receive a structure, the Compiler will know the size from the declaration. The Windows Standard way of dealing with pointers to structures of variable size is by using a member c_Bytes as the first member of a structure. This member must be guaranteed to always be initialised to the size of the structure. However, this is only the top of the iceberg, the problem is far larger, particularly if you deal with data structures generated by different compilers. For starters there is the Packing Method. The Structure {char Name[5],PhoneNumber[7]}; would by default be compiled into an object of 16 (NOT 12) bytes. An extra 3 bytes would be placed after Name to make PhoneNumber start at a DWORD boundary, and an extra byte would then be added to the entire structure, so that an array of it would have each element on a DWORD boundary. You can control this behaviour with a #pragma pack in the header file that declares the structure. (packing for a structure is set at Declaration level, NOT at Useage level). #pragma pack(1) would indeed create a 12 byte structure. Another issue is when you use structures generated by different compilers are dependent on issues of the kind of 'Howmany bytes does an int occupy'. You can circumvent a lot of this by using WORD and DWORD types, which most( but not All) compiler writers agree occupy 2 resp 4 bytes. Do not forget, an int under WIN16 was 2 Bytes, under WIN32 it is 4. So, the same Structure Declaration will lead to entrely different Sizes and Offsets under WIN16 compared to WIN32. Returning to your original question: Is there a (generic)way of determining the Size of a Structure without using the sizeof operator, the answer is No, Definitely Not. sizeof is not a function, it is a reserved name which the compiler at compile time resolves to the size of the object by reference to the symbol table. If the size is not known at compile time, the compiler provides NO alternative mechanism, you must create your own runtime mechanism to transmit the size of the object a pointer points to inside the object.

                            LateNightsInNewry

                            D 1 Reply Last reply
                            0
                            • B Bram van Kampen

                              There is some crap being posted here. The only time this is an issue is when you receive a pointer to a structure. If you receive a structure, the Compiler will know the size from the declaration. The Windows Standard way of dealing with pointers to structures of variable size is by using a member c_Bytes as the first member of a structure. This member must be guaranteed to always be initialised to the size of the structure. However, this is only the top of the iceberg, the problem is far larger, particularly if you deal with data structures generated by different compilers. For starters there is the Packing Method. The Structure {char Name[5],PhoneNumber[7]}; would by default be compiled into an object of 16 (NOT 12) bytes. An extra 3 bytes would be placed after Name to make PhoneNumber start at a DWORD boundary, and an extra byte would then be added to the entire structure, so that an array of it would have each element on a DWORD boundary. You can control this behaviour with a #pragma pack in the header file that declares the structure. (packing for a structure is set at Declaration level, NOT at Useage level). #pragma pack(1) would indeed create a 12 byte structure. Another issue is when you use structures generated by different compilers are dependent on issues of the kind of 'Howmany bytes does an int occupy'. You can circumvent a lot of this by using WORD and DWORD types, which most( but not All) compiler writers agree occupy 2 resp 4 bytes. Do not forget, an int under WIN16 was 2 Bytes, under WIN32 it is 4. So, the same Structure Declaration will lead to entrely different Sizes and Offsets under WIN16 compared to WIN32. Returning to your original question: Is there a (generic)way of determining the Size of a Structure without using the sizeof operator, the answer is No, Definitely Not. sizeof is not a function, it is a reserved name which the compiler at compile time resolves to the size of the object by reference to the symbol table. If the size is not known at compile time, the compiler provides NO alternative mechanism, you must create your own runtime mechanism to transmit the size of the object a pointer points to inside the object.

                              LateNightsInNewry

                              D Offline
                              D Offline
                              DevMentor org
                              wrote on last edited by
                              #14

                              >>There is some crap being posted here. And you're validating the posted crap with your long winded diatribe ;P

                              Yours Truly, The One and Only!

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups