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. Size of an empty class

Size of an empty class

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 8 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.
  • P Offline
    P Offline
    pix_programmer
    wrote on last edited by
    #1

    Hi! What's the size of an empty class? When I execute the following code:

    #include "iostream"
    using namespace std;
    class test
    {
    };
    void main()
    {
    test test1;
    std::cout<

    it prints 1. How it prints 1 with out any variable being declared?

    M T _ S 4 Replies Last reply
    0
    • P pix_programmer

      Hi! What's the size of an empty class? When I execute the following code:

      #include "iostream"
      using namespace std;
      class test
      {
      };
      void main()
      {
      test test1;
      std::cout<

      it prints 1. How it prints 1 with out any variable being declared?

      M Offline
      M Offline
      markkuk
      wrote on last edited by
      #2

      An object must have non-zero size: http://www2.research.att.com/~bs/bs_faq2.html#sizeof-empty[^]

      J 1 Reply Last reply
      0
      • P pix_programmer

        Hi! What's the size of an empty class? When I execute the following code:

        #include "iostream"
        using namespace std;
        class test
        {
        };
        void main()
        {
        test test1;
        std::cout<

        it prints 1. How it prints 1 with out any variable being declared?

        T Offline
        T Offline
        T2102
        wrote on last edited by
        #3

        C++ needs to be able to create pointers to arbitrary objects for inheritance. You could create a class OBJECT that is a base class that you derive from.

        1 Reply Last reply
        0
        • P pix_programmer

          Hi! What's the size of an empty class? When I execute the following code:

          #include "iostream"
          using namespace std;
          class test
          {
          };
          void main()
          {
          test test1;
          std::cout<

          it prints 1. How it prints 1 with out any variable being declared?

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          The answer to your question is in the link provided by markkuk. You can actually store a one byte character in an empty class.

          class test
          {
          };

          void main()
          {
          test test1;
          *((char*)&test1) = 'A';
          }

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          E 1 Reply Last reply
          0
          • _ _Superman_

            The answer to your question is in the link provided by markkuk. You can actually store a one byte character in an empty class.

            class test
            {
            };

            void main()
            {
            test test1;
            *((char*)&test1) = 'A';
            }

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++)

            Polymorphism in C

            E Offline
            E Offline
            Emilio Garavaglia
            wrote on last edited by
            #5

            Dangerous! The standard says tat distinct objects must have distinct identity (and if the identity is the address, a common way to get that is posing sizeof(A) = 1 by definition). But this is ... compiler dependent. (they can even use 4 bytes to preserve the alignment!) An Empty B inheriting the empty A may still have sizeof(B) == 1, not 2.

            2 bugs found. > recompile ... 65534 bugs found. :doh:

            C 1 Reply Last reply
            0
            • E Emilio Garavaglia

              Dangerous! The standard says tat distinct objects must have distinct identity (and if the identity is the address, a common way to get that is posing sizeof(A) = 1 by definition). But this is ... compiler dependent. (they can even use 4 bytes to preserve the alignment!) An Empty B inheriting the empty A may still have sizeof(B) == 1, not 2.

              2 bugs found. > recompile ... 65534 bugs found. :doh:

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

              Why is it dangerous (maybe I didn't get you)? :)

              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.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              E 1 Reply Last reply
              0
              • C CPallini

                Why is it dangerous (maybe I didn't get you)? :)

                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.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                E Offline
                E Offline
                Emilio Garavaglia
                wrote on last edited by
                #7

                Because what Santosh is trying to do is assign a char to a byte that it is not granted to really exist. (it may or not depending on the compiler implementation and level of optimization)

                2 bugs found. > recompile ... 65534 bugs found. :doh:

                C 1 Reply Last reply
                0
                • E Emilio Garavaglia

                  Because what Santosh is trying to do is assign a char to a byte that it is not granted to really exist. (it may or not depending on the compiler implementation and level of optimization)

                  2 bugs found. > recompile ... 65534 bugs found. :doh:

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

                  I'm almost sure you're right (I cannot see other ways to implement it, but this is a problem of mine). Thank you. :)

                  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.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • P pix_programmer

                    Hi! What's the size of an empty class? When I execute the following code:

                    #include "iostream"
                    using namespace std;
                    class test
                    {
                    };
                    void main()
                    {
                    test test1;
                    std::cout<

                    it prints 1. How it prints 1 with out any variable being declared?

                    S Offline
                    S Offline
                    Shivanand Gupta
                    wrote on last edited by
                    #9

                    Dear, empty class does not have any size. but size of operator always return +ve value. so you get empty class size 1.

                    1 Reply Last reply
                    0
                    • M markkuk

                      An object must have non-zero size: http://www2.research.att.com/~bs/bs_faq2.html#sizeof-empty[^]

                      J Offline
                      J Offline
                      jk chan
                      wrote on last edited by
                      #10

                      OR i can say in otherwords like opertor sizeof never returns ZERO :-D

                      If u can Dream... U can do it

                      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