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. One question about size of empty class...

One question about size of empty class...

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structures
10 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.
  • A Offline
    A Offline
    asadullah ansari
    wrote on last edited by
    #1

    Dont bore of this 80's question...please excuse me for this... question is very simple... Why empty class's size is one Byte in C++??? Answer 1:suppose in case of creating array of objects of this empty class, then to distinguish them atleast 1 byte is required ... mE fedup of this answer My Doubt for this answer : If this reason is valid then why hell in C, empty structure's size is zero... Here also anyone can create the array of objects of this empty structure.Here no neeed to distinguise. Is It ???? Answer 2: Since in C++ provides function members so like class test { }; this is also empty class but by default every class has some default member's function... test tObject1=test(); test tObject2(tObject1); or class test { public: void fun(); }; test obj; obj.fun(); so to call this function it may required size atleast 1 byte... BUT I M NOT SUTE FOR WHAT REASON IN C++ THEY HAVE TAKEN 1 BYTE FOR EMPTY CLASS AND ZERO BYTE FOR C....PLEASE U CAN SHARE WHAT'S EXACT REASON????

    Truth Can'nt be changed

    C L _ A K 5 Replies Last reply
    0
    • A asadullah ansari

      Dont bore of this 80's question...please excuse me for this... question is very simple... Why empty class's size is one Byte in C++??? Answer 1:suppose in case of creating array of objects of this empty class, then to distinguish them atleast 1 byte is required ... mE fedup of this answer My Doubt for this answer : If this reason is valid then why hell in C, empty structure's size is zero... Here also anyone can create the array of objects of this empty structure.Here no neeed to distinguise. Is It ???? Answer 2: Since in C++ provides function members so like class test { }; this is also empty class but by default every class has some default member's function... test tObject1=test(); test tObject2(tObject1); or class test { public: void fun(); }; test obj; obj.fun(); so to call this function it may required size atleast 1 byte... BUT I M NOT SUTE FOR WHAT REASON IN C++ THEY HAVE TAKEN 1 BYTE FOR EMPTY CLASS AND ZERO BYTE FOR C....PLEASE U CAN SHARE WHAT'S EXACT REASON????

      Truth Can'nt be changed

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      asadullah ansari wrote:

      THEY HAVE TAKEN 1 BYTE FOR EMPTY CLASS AND ZERO BYTE FOR C

      C++ != C and a C++ class is not a C struct the C++ standard requires that all class objects have a "nonzero size". see page 149 : http://www-d0.fnal.gov/~dladams/cxx_standard.pdf[^]

      image processing toolkits | batch image processing

      A 1 Reply Last reply
      0
      • A asadullah ansari

        Dont bore of this 80's question...please excuse me for this... question is very simple... Why empty class's size is one Byte in C++??? Answer 1:suppose in case of creating array of objects of this empty class, then to distinguish them atleast 1 byte is required ... mE fedup of this answer My Doubt for this answer : If this reason is valid then why hell in C, empty structure's size is zero... Here also anyone can create the array of objects of this empty structure.Here no neeed to distinguise. Is It ???? Answer 2: Since in C++ provides function members so like class test { }; this is also empty class but by default every class has some default member's function... test tObject1=test(); test tObject2(tObject1); or class test { public: void fun(); }; test obj; obj.fun(); so to call this function it may required size atleast 1 byte... BUT I M NOT SUTE FOR WHAT REASON IN C++ THEY HAVE TAKEN 1 BYTE FOR EMPTY CLASS AND ZERO BYTE FOR C....PLEASE U CAN SHARE WHAT'S EXACT REASON????

        Truth Can'nt be changed

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        that is what ++ in C++ means: add 1. :laugh:

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        A 1 Reply Last reply
        0
        • C Chris Losinger

          asadullah ansari wrote:

          THEY HAVE TAKEN 1 BYTE FOR EMPTY CLASS AND ZERO BYTE FOR C

          C++ != C and a C++ class is not a C struct the C++ standard requires that all class objects have a "nonzero size". see page 149 : http://www-d0.fnal.gov/~dladams/cxx_standard.pdf[^]

          image processing toolkits | batch image processing

          A Offline
          A Offline
          asadullah ansari
          wrote on last edited by
          #4

          offcourse C != C++ ...Here offcourse C++ standard requires it so it's there But my question is why they have made like this... Some reason should be for this...

          Truth Can'nt be changed

          L E 2 Replies Last reply
          0
          • L Luc Pattyn

            that is what ++ in C++ means: add 1. :laugh:

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            A Offline
            A Offline
            asadullah ansari
            wrote on last edited by
            #5

            reallly funny ...gud :laugh:

            Truth Can'nt be changed

            1 Reply Last reply
            0
            • A asadullah ansari

              offcourse C != C++ ...Here offcourse C++ standard requires it so it's there But my question is why they have made like this... Some reason should be for this...

              Truth Can'nt be changed

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              asadullah ansari wrote:

              Some reason

              because same pointers should imply same objects (and same types), different pointer values different objects. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              1 Reply Last reply
              0
              • A asadullah ansari

                Dont bore of this 80's question...please excuse me for this... question is very simple... Why empty class's size is one Byte in C++??? Answer 1:suppose in case of creating array of objects of this empty class, then to distinguish them atleast 1 byte is required ... mE fedup of this answer My Doubt for this answer : If this reason is valid then why hell in C, empty structure's size is zero... Here also anyone can create the array of objects of this empty structure.Here no neeed to distinguise. Is It ???? Answer 2: Since in C++ provides function members so like class test { }; this is also empty class but by default every class has some default member's function... test tObject1=test(); test tObject2(tObject1); or class test { public: void fun(); }; test obj; obj.fun(); so to call this function it may required size atleast 1 byte... BUT I M NOT SUTE FOR WHAT REASON IN C++ THEY HAVE TAKEN 1 BYTE FOR EMPTY CLASS AND ZERO BYTE FOR C....PLEASE U CAN SHARE WHAT'S EXACT REASON????

                Truth Can'nt be changed

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

                Here is what the man himself has to say about it - Why is the size of an empty class not zero?[^]

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

                Microsoft MVP (Visual C++)

                Polymorphism in C

                1 Reply Last reply
                0
                • A asadullah ansari

                  Dont bore of this 80's question...please excuse me for this... question is very simple... Why empty class's size is one Byte in C++??? Answer 1:suppose in case of creating array of objects of this empty class, then to distinguish them atleast 1 byte is required ... mE fedup of this answer My Doubt for this answer : If this reason is valid then why hell in C, empty structure's size is zero... Here also anyone can create the array of objects of this empty structure.Here no neeed to distinguise. Is It ???? Answer 2: Since in C++ provides function members so like class test { }; this is also empty class but by default every class has some default member's function... test tObject1=test(); test tObject2(tObject1); or class test { public: void fun(); }; test obj; obj.fun(); so to call this function it may required size atleast 1 byte... BUT I M NOT SUTE FOR WHAT REASON IN C++ THEY HAVE TAKEN 1 BYTE FOR EMPTY CLASS AND ZERO BYTE FOR C....PLEASE U CAN SHARE WHAT'S EXACT REASON????

                  Truth Can'nt be changed

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #8

                  Not being the creator of the language I can't answer definitively so I'll just refer you to the "Design and Evolution of C++" by Bjarne Stroustrup. I hope you won't be disapointed by the answer! Cheers, Ash

                  1 Reply Last reply
                  0
                  • A asadullah ansari

                    Dont bore of this 80's question...please excuse me for this... question is very simple... Why empty class's size is one Byte in C++??? Answer 1:suppose in case of creating array of objects of this empty class, then to distinguish them atleast 1 byte is required ... mE fedup of this answer My Doubt for this answer : If this reason is valid then why hell in C, empty structure's size is zero... Here also anyone can create the array of objects of this empty structure.Here no neeed to distinguise. Is It ???? Answer 2: Since in C++ provides function members so like class test { }; this is also empty class but by default every class has some default member's function... test tObject1=test(); test tObject2(tObject1); or class test { public: void fun(); }; test obj; obj.fun(); so to call this function it may required size atleast 1 byte... BUT I M NOT SUTE FOR WHAT REASON IN C++ THEY HAVE TAKEN 1 BYTE FOR EMPTY CLASS AND ZERO BYTE FOR C....PLEASE U CAN SHARE WHAT'S EXACT REASON????

                    Truth Can'nt be changed

                    K Offline
                    K Offline
                    KarstenK
                    wrote on last edited by
                    #9

                    to call a function you need a memory address for the function pointer. So you need the size of a void*. Typically 4 Bytes.

                    Press F1 for help or google it. Greetings from Germany

                    1 Reply Last reply
                    0
                    • A asadullah ansari

                      offcourse C != C++ ...Here offcourse C++ standard requires it so it's there But my question is why they have made like this... Some reason should be for this...

                      Truth Can'nt be changed

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

                      The reason is very subjective. In the below posts you can find the link to Stroustroup site, and his "official" answer. The interesting thing is that the argumentation he provides are good for C as well. IMHO, I think it was just a different "taste" between K&R and B.S. In C a struct is just a "bouch of bytes" and and address is just a "place in memory". In C++ a struct is an "object" and an address is an "object identity" (and two objects must not have the same one). Of course we can discuss if C++ classes and structs are "real" object or just tools to assemble them ... but it is still another matter of "taste".

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

                      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