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
questiontestingbeta-testingcareer
8 Posts 5 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
    avenger_sb25
    wrote on last edited by
    #1

    Hi einstines, i was asked this question at an interview: class a { }; main() { cout<//WHATS THE OUTPUT? } I can obviously run the program to find the output but i need an explanation about the output. PLEASE ANSWER: WHAT IS THE OUTPUT???? ...Avenger


    Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

    J D N 3 Replies Last reply
    0
    • A avenger_sb25

      Hi einstines, i was asked this question at an interview: class a { }; main() { cout<//WHATS THE OUTPUT? } I can obviously run the program to find the output but i need an explanation about the output. PLEASE ANSWER: WHAT IS THE OUTPUT???? ...Avenger


      Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

      J Offline
      J Offline
      jmkhael
      wrote on last edited by
      #2

      i did just that in VC++ class A { }; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int i = sizeof(A); cout << "Sizeof A:" << i << endl; } it displayed 1. I switched to assebmly listing and the linker has generated the following code: 77: int i = sizeof(A); 004018AB mov dword ptr [i],1 <----- Notice here, the linker knew its an empty class and automatically replaced its size by 1 78: 79: cout << "Sizeof A:" << i << endl; 004018B2 mov esi,esp 004018B4 mov edx,dword ptr [__imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z 004018BA push edx 004018BB mov edi,esp 004018BD mov eax,dword ptr [i] 004018C0 push eax 004018C1 mov ebx,esp 004018C3 push offset string "Sizeof A:" (004130d4) 004018C8 mov ecx,dword ptr [MSVCP60D_NULL_THUNK_DATA (00415234)] 004018CE push ecx 004018CF call dword ptr [__imp_??6std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z (004 004018D5 add esp,8 004018D8 cmp ebx,esp 004018DA call _chkesp (004014bc) 004018DF mov ecx,eax 004018E1 call dword ptr [__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z (0041523c) 004018E7 cmp edi,esp 004018E9 call _chkesp (004014bc) 004018EE mov ecx,eax 004018F0 call dword ptr [__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01 004018F6 cmp esi,esp 004018F8 call _chkesp (004014bc) Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      A 1 Reply Last reply
      0
      • J jmkhael

        i did just that in VC++ class A { }; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int i = sizeof(A); cout << "Sizeof A:" << i << endl; } it displayed 1. I switched to assebmly listing and the linker has generated the following code: 77: int i = sizeof(A); 004018AB mov dword ptr [i],1 <----- Notice here, the linker knew its an empty class and automatically replaced its size by 1 78: 79: cout << "Sizeof A:" << i << endl; 004018B2 mov esi,esp 004018B4 mov edx,dword ptr [__imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z 004018BA push edx 004018BB mov edi,esp 004018BD mov eax,dword ptr [i] 004018C0 push eax 004018C1 mov ebx,esp 004018C3 push offset string "Sizeof A:" (004130d4) 004018C8 mov ecx,dword ptr [MSVCP60D_NULL_THUNK_DATA (00415234)] 004018CE push ecx 004018CF call dword ptr [__imp_??6std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z (004 004018D5 add esp,8 004018D8 cmp ebx,esp 004018DA call _chkesp (004014bc) 004018DF mov ecx,eax 004018E1 call dword ptr [__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z (0041523c) 004018E7 cmp edi,esp 004018E9 call _chkesp (004014bc) 004018EE mov ecx,eax 004018F0 call dword ptr [__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01 004018F6 cmp esi,esp 004018F8 call _chkesp (004014bc) Papa while (TRUE) Papa.WillLove ( Bebe ) ;

        A Offline
        A Offline
        avenger_sb25
        wrote on last edited by
        #3

        Ya, thanx for ur reply and to tell the technique to see linker output ;) (thats a new thing i learnt) But, why 1? is it property of the linker or a C++ standard to make size of an empty class as 1. Please tell. ...Avenger


        Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

        J 1 Reply Last reply
        0
        • A avenger_sb25

          Ya, thanx for ur reply and to tell the technique to see linker output ;) (thats a new thing i learnt) But, why 1? is it property of the linker or a C++ standard to make size of an empty class as 1. Please tell. ...Avenger


          Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

          J Offline
          J Offline
          jmkhael
          wrote on last edited by
          #4

          Never tested it on unix, but one thing im sure of, is that empty structs are forbidden if you compile the file as a c file and not as a cpp. However while permitting generation of an empty class, the linker is forced to allocate some space for it (vtable ...) and cos its empty, sizeof just returned the minimal size reserved for a class. Papa while (TRUE) Papa.WillLove ( Bebe ) ;

          C 1 Reply Last reply
          0
          • A avenger_sb25

            Hi einstines, i was asked this question at an interview: class a { }; main() { cout<//WHATS THE OUTPUT? } I can obviously run the program to find the output but i need an explanation about the output. PLEASE ANSWER: WHAT IS THE OUTPUT???? ...Avenger


            Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

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

            avenger_sb25 wrote: WHAT IS THE OUTPUT???? It won't be zero, as that would allow two different objects to have the same address. If sizeof(a) returned 0, then the following would be allowed:

            main()
            {
            a a1, a2;
            if (&a1 == &a2)
            ... // if we get here, the compiler is faulty
            }


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            1 Reply Last reply
            0
            • A avenger_sb25

              Hi einstines, i was asked this question at an interview: class a { }; main() { cout<//WHATS THE OUTPUT? } I can obviously run the program to find the output but i need an explanation about the output. PLEASE ANSWER: WHAT IS THE OUTPUT???? ...Avenger


              Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              From the Standard (1.8.5) Unless it is a bitfield, a most derived object shell have a non-zero size and shell occupy one ore more bytes of storage. Base class sub-objects may have zero size.

              A 1 Reply Last reply
              0
              • J jmkhael

                Never tested it on unix, but one thing im sure of, is that empty structs are forbidden if you compile the file as a c file and not as a cpp. However while permitting generation of an empty class, the linker is forced to allocate some space for it (vtable ...) and cos its empty, sizeof just returned the minimal size reserved for a class. Papa while (TRUE) Papa.WillLove ( Bebe ) ;

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

                VTable is only allocated if the class actually contains virtual functions. The size of an empty class will always be greater than zero because two objects must not occupy the same space. (As DavidCrow pointed out)

                1 Reply Last reply
                0
                • N Nemanja Trifunovic

                  From the Standard (1.8.5) Unless it is a bitfield, a most derived object shell have a non-zero size and shell occupy one ore more bytes of storage. Base class sub-objects may have zero size.

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

                  Base class sub-objects may have zero size Can u please give a working example. I did class p { }; class q: public p { }; p o1; q o2; cout< I get 1 in both. Thank you. ...Avenger * * * Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

                  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