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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. An unexpected output

An unexpected output

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 3 Posters 1 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.
  • C Offline
    C Offline
    carter2000
    wrote on last edited by
    #1

    #include <iostream> using namespace std; class CTestA { public: virtual void FuncA() {} private: int m_iValue1; char m_cValue1; int m_iValue2; char m_cValue2; double m_dValue1; }; class CTestB : public CTestA { public: virtual void FuncB() {} private: }; void main() { cout << sizeof(CTestB) << endl; system("pause"); } Why the output is '32', while my expected is '28'.

    N S 2 Replies Last reply
    0
    • C carter2000

      #include <iostream> using namespace std; class CTestA { public: virtual void FuncA() {} private: int m_iValue1; char m_cValue1; int m_iValue2; char m_cValue2; double m_dValue1; }; class CTestB : public CTestA { public: virtual void FuncB() {} private: }; void main() { cout << sizeof(CTestB) << endl; system("pause"); } Why the output is '32', while my expected is '28'.

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      carter2000 wrote:

      Why the output is '32', while my expected is '28'.

      How are you calculating 28? By adding size of individual members? If yes, see what MSDN is saying. When the sizeof operator is applied to a class, struct, or union type, the result is the number of bytes in an object of that type, plus any padding added to align members on word boundaries. The result does not necessarily correspond to the size calculated by adding the storage requirements of the individual members.

      Navaneeth How to use google | Ask smart questions

      C 1 Reply Last reply
      0
      • C carter2000

        #include <iostream> using namespace std; class CTestA { public: virtual void FuncA() {} private: int m_iValue1; char m_cValue1; int m_iValue2; char m_cValue2; double m_dValue1; }; class CTestB : public CTestA { public: virtual void FuncB() {} private: }; void main() { cout << sizeof(CTestB) << endl; system("pause"); } Why the output is '32', while my expected is '28'.

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        Structure sizes don't work that way - there may be (will be in this case) be padding between items of different types in a structure, dependent on the type's default alignment. Have a look at this Wikipedia page[^] that explains the concept. However, there is a way around the default alignments, using #pragma pack to alter structure packing. So, if you put the following line before your structure definitions, you'll find you get the output you expect:

        #pragma pack(1)

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        C 1 Reply Last reply
        0
        • N N a v a n e e t h

          carter2000 wrote:

          Why the output is '32', while my expected is '28'.

          How are you calculating 28? By adding size of individual members? If yes, see what MSDN is saying. When the sizeof operator is applied to a class, struct, or union type, the result is the number of bytes in an object of that type, plus any padding added to align members on word boundaries. The result does not necessarily correspond to the size calculated by adding the storage requirements of the individual members.

          Navaneeth How to use google | Ask smart questions

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

          Thanks for your answer.

          1 Reply Last reply
          0
          • S Stuart Dootson

            Structure sizes don't work that way - there may be (will be in this case) be padding between items of different types in a structure, dependent on the type's default alignment. Have a look at this Wikipedia page[^] that explains the concept. However, there is a way around the default alignments, using #pragma pack to alter structure packing. So, if you put the following line before your structure definitions, you'll find you get the output you expect:

            #pragma pack(1)

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            C Offline
            C Offline
            carter2000
            wrote on last edited by
            #5

            Thanks. I add "#pragma pack(4)", and it works as I wish.

            S 1 Reply Last reply
            0
            • C carter2000

              Thanks. I add "#pragma pack(4)", and it works as I wish.

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              Not #pragma pack(1)?

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              C 1 Reply Last reply
              0
              • S Stuart Dootson

                Not #pragma pack(1)?

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

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

                Yes, not #pragma pack(1). I put it in my program, and the output was "22". :)

                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