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. Polymorphism in C++

Polymorphism in C++

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++delphicomoop
6 Posts 6 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.
  • G Offline
    G Offline
    gutsyfarid
    wrote on last edited by
    #1

    PROBLEM Unable to understand the variation in size of object All the code is tested under Borland C++ ver 3.1 In borland V3.1 the int takes two bytes Lets look at the following code Class base { public: int b; base() {} virtual void f() {} virtual void g() {} }; Class derive : virtual public base { public: int d; derive() {} virtual void f() {} virtual void g() {} }; ……………… derive d; cout<gives 12 why? /* But if I don’t give constructors in derive then the size of derive class object is 10, because there will be no 2 bytes extra in object layout as shown below. Please help me out about finding the myth of these extra two bytes. */

     Object-Layout            V-TABLE
        |------------|          |------------|
     ---|ptr to base |       -->|derive::f   |	
     |  |------------|       |  |------------|
     |  |derive v-ptr|-------|  |derive::k   | 
     |  |------------|          |------------|
     |  |d           |        ->|derive::f   |
     |  |------------|        | |------------|
     |  |0           |<--     | |base::g     |
     |  |------------|   |    | |------------|
     -->|base-vptr   |---|----|	
        |------------|   |	
        |b           |   |
        |------------|   |**what the hell is this**
    

    Please reply at: Contact: informfarid@yahoo.com ------------------------------------------------------------ In the middle of dificulty lies oppurtunity.

    J P B 3 Replies Last reply
    0
    • G gutsyfarid

      PROBLEM Unable to understand the variation in size of object All the code is tested under Borland C++ ver 3.1 In borland V3.1 the int takes two bytes Lets look at the following code Class base { public: int b; base() {} virtual void f() {} virtual void g() {} }; Class derive : virtual public base { public: int d; derive() {} virtual void f() {} virtual void g() {} }; ……………… derive d; cout<gives 12 why? /* But if I don’t give constructors in derive then the size of derive class object is 10, because there will be no 2 bytes extra in object layout as shown below. Please help me out about finding the myth of these extra two bytes. */

       Object-Layout            V-TABLE
          |------------|          |------------|
       ---|ptr to base |       -->|derive::f   |	
       |  |------------|       |  |------------|
       |  |derive v-ptr|-------|  |derive::k   | 
       |  |------------|          |------------|
       |  |d           |        ->|derive::f   |
       |  |------------|        | |------------|
       |  |0           |<--     | |base::g     |
       |  |------------|   |    | |------------|
       -->|base-vptr   |---|----|	
          |------------|   |	
          |b           |   |
          |------------|   |**what the hell is this**
      

      Please reply at: Contact: informfarid@yahoo.com ------------------------------------------------------------ In the middle of dificulty lies oppurtunity.

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

      Can I ask why you need to know? A compiler is at liberty to decide how to layout a class - the C++ standard doesnt define this. I recall finding a description of different methods on the net, so you should be able to find the details from google. I would imagine the extra byte is for some internal use by the Borland compiler, or maybe even just for padding. Either way, you shouldnt base code upon the size of an object, as compilers will give different sizes.

      M 1 Reply Last reply
      0
      • J Johnny

        Can I ask why you need to know? A compiler is at liberty to decide how to layout a class - the C++ standard doesnt define this. I recall finding a description of different methods on the net, so you should be able to find the details from google. I would imagine the extra byte is for some internal use by the Borland compiler, or maybe even just for padding. Either way, you shouldnt base code upon the size of an object, as compilers will give different sizes.

        M Offline
        M Offline
        mfareed_baig
        wrote on last edited by
        #3

        2 extra bytes meanse shortage of ram Even if there is know reason one should know the myth of this mystry ------------------------------------------------------------You miss 100% of shots that u dont take.

        J 1 Reply Last reply
        0
        • G gutsyfarid

          PROBLEM Unable to understand the variation in size of object All the code is tested under Borland C++ ver 3.1 In borland V3.1 the int takes two bytes Lets look at the following code Class base { public: int b; base() {} virtual void f() {} virtual void g() {} }; Class derive : virtual public base { public: int d; derive() {} virtual void f() {} virtual void g() {} }; ……………… derive d; cout<gives 12 why? /* But if I don’t give constructors in derive then the size of derive class object is 10, because there will be no 2 bytes extra in object layout as shown below. Please help me out about finding the myth of these extra two bytes. */

           Object-Layout            V-TABLE
              |------------|          |------------|
           ---|ptr to base |       -->|derive::f   |	
           |  |------------|       |  |------------|
           |  |derive v-ptr|-------|  |derive::k   | 
           |  |------------|          |------------|
           |  |d           |        ->|derive::f   |
           |  |------------|        | |------------|
           |  |0           |<--     | |base::g     |
           |  |------------|   |    | |------------|
           -->|base-vptr   |---|----|	
              |------------|   |	
              |b           |   |
              |------------|   |**what the hell is this**
          

          Please reply at: Contact: informfarid@yahoo.com ------------------------------------------------------------ In the middle of dificulty lies oppurtunity.

          P Offline
          P Offline
          Paul Ranson
          wrote on last edited by
          #4

          I think it's related to your use of virtual inheritance. What's your reason for making the inheritance relationship 'virtual'? Paul

          1 Reply Last reply
          0
          • M mfareed_baig

            2 extra bytes meanse shortage of ram Even if there is know reason one should know the myth of this mystry ------------------------------------------------------------You miss 100% of shots that u dont take.

            J Offline
            J Offline
            Johan Rosengren
            wrote on last edited by
            #5

            Saving 2 bytes! You must be kidding - for a 1 000 instances of the class, this means (less than) a meagre 2 kbs of wasted space. This is not uncommon - "wasting" memory, that is - structs will be padded, for example, for reasons of performance. You should just leave this to the compiler-writers, madness lies in this direction :-)

            1 Reply Last reply
            0
            • G gutsyfarid

              PROBLEM Unable to understand the variation in size of object All the code is tested under Borland C++ ver 3.1 In borland V3.1 the int takes two bytes Lets look at the following code Class base { public: int b; base() {} virtual void f() {} virtual void g() {} }; Class derive : virtual public base { public: int d; derive() {} virtual void f() {} virtual void g() {} }; ……………… derive d; cout<gives 12 why? /* But if I don’t give constructors in derive then the size of derive class object is 10, because there will be no 2 bytes extra in object layout as shown below. Please help me out about finding the myth of these extra two bytes. */

               Object-Layout            V-TABLE
                  |------------|          |------------|
               ---|ptr to base |       -->|derive::f   |	
               |  |------------|       |  |------------|
               |  |derive v-ptr|-------|  |derive::k   | 
               |  |------------|          |------------|
               |  |d           |        ->|derive::f   |
               |  |------------|        | |------------|
               |  |0           |<--     | |base::g     |
               |  |------------|   |    | |------------|
               -->|base-vptr   |---|----|	
                  |------------|   |	
                  |b           |   |
                  |------------|   |**what the hell is this**
              

              Please reply at: Contact: informfarid@yahoo.com ------------------------------------------------------------ In the middle of dificulty lies oppurtunity.

              B Offline
              B Offline
              Blake Miller
              wrote on last edited by
              #6

              It may have everything to do with keeping VTables on 4-byte boundaries. Since VTables will resolve into pointers to functions in derived classes, they want to keep them aligned so the performance on the CPU is better. As an experiment, you can try changing public: int b; base() into public: byte b; base() And you will still probably get 12. Then try public: int b; int c; base() And you might still get 12. I think it has do do with optimizing pointers to tables of function pointers, myself.

              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