Problem with sizeof, when using it with Base class pointer

Discussion in 'C++' started by Gopal-M, Jun 1, 2004.

  1. Gopal-M

    Gopal-M Guest

    I have the problem with sizeof operator I also want to implement a
    function that can return size of an object. My problem is as follows..

    I have a Base class, say Base and there are many class derived from
    it. At a particular point in my application I need the size of the
    object pointed to by the base pointer. Eg





    Class Base

    {
    private:

    int m_nBaseData;

    virtual void Display() = 0;

    };



    class Dev1: public Base
    {
    private:
    int m_nDev1Data[10];
    public:
    void Display()
    {
    /// ……. Code..
    }
    };

    class Dev2: public Base
    {
    private:
    int m_nDev2Data[10];
    public:
    void Display()
    {
    /// ……. Code..
    }
    };


    void main()
    {
    Base* p1 = new Dev1;
    Base* p2 = new Dev2;

    unsigned int size1 = sizeof(*p1); // LINE # 1

    unsigned int size2 = sizeof(*p2); // LINE # 2
    }



    As the code given above I am getting size as 8 in LINE # 1 and LINE #
    2, it is the size of the Base class.

    In my application I need the size of the object pointed to by the
    pointer. Means, if the base pointer contains the Dev1 obj then i
    should some how need the size of the object pointed by Base pointer.
    Can any one give me any solution for this.



    Gopal.
    Lambent Technologies
    Gopal-M, Jun 1, 2004
    #1
    1. Advertising

  2. Gopal-M wrote:
    > I have the problem with sizeof operator I also want to implement a
    > function that can return size of an object. My problem is as follows..
    >
    > I have a Base class, say Base and there are many class derived from
    > it. At a particular point in my application I need the size of the
    > object pointed to by the base pointer. Eg
    >
    >
    >
    >
    >
    > Class Base
    >
    > {
    > private:
    >
    > int m_nBaseData;
    >
    > virtual void Display() = 0;
    >
    > };
    >
    >
    >
    > class Dev1: public Base
    > {
    > private:
    > int m_nDev1Data[10];
    > public:
    > void Display()
    > {
    > /// ……. Code..
    > }
    > };
    >
    > class Dev2: public Base
    > {
    > private:
    > int m_nDev2Data[10];
    > public:
    > void Display()
    > {
    > /// ……. Code..
    > }
    > };
    >
    >
    > void main()
    > {
    > Base* p1 = new Dev1;
    > Base* p2 = new Dev2;
    >
    > unsigned int size1 = sizeof(*p1); // LINE # 1
    >
    > unsigned int size2 = sizeof(*p2); // LINE # 2
    > }
    >
    >
    >
    > As the code given above I am getting size as 8 in LINE # 1 and LINE #
    > 2, it is the size of the Base class.
    >
    > In my application I need the size of the object pointed to by the
    > pointer. Means, if the base pointer contains the Dev1 obj then i
    > should some how need the size of the object pointed by Base pointer.
    > Can any one give me any solution for this.


    'sizeof' is a compile-time "operator". Since the result of the "size
    of the actual object" depends on the size of the run-time _real_ object,
    there is no way for 'sizeof' to properly solve this. The only way
    you could attempt to fix this is to use a virtual function in 'Base'
    that reports the size:

    class Base {
    ...
    virtual std::size_t getSize() const = 0; // pure, to force
    // implementation
    };

    // implementation -- just in case
    inline std::size_t Base::getSize() const { return sizeof(Base); }

    class Derived1 : public Base {
    ...
    std::size_t getSize() const { return sizeof(Derived1); }
    };

    ....

    Victor
    Victor Bazarov, Jun 1, 2004
    #2
    1. Advertising

  3. Gopal-M

    David Harmon Guest

    On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++,
    (Gopal-M) wrote,
    >I have a Base class, say Base and there are many class derived from
    >it. At a particular point in my application I need the size of the
    >object pointed to by the base pointer. Eg


    You should write your code so that it uses only the declared public
    interface of your classes. The size of the object typically is not,
    and should not be, anything for foreign code to depend upon.
    David Harmon, Jun 1, 2004
    #3
  4. Gopal-M

    Gopal-M Guest

    David Harmon <> wrote in message news:<>...
    > On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++,
    > (Gopal-M) wrote,
    > >I have a Base class, say Base and there are many class derived from
    > >it. At a particular point in my application I need the size of the
    > >object pointed to by the base pointer. Eg

    >
    > You should write your code so that it uses only the declared public
    > interface of your classes. The size of the object typically is not,
    > and should not be, anything for foreign code to depend upon.


    Thanks David and Victor for u r Suggestions. The problem is
    that i can not demand other module devlopers to implement a getSize()
    in their module just to return the sizeof their class; which is of no
    use to their objects and as i am implementing the memory management
    module, memory management should be handeled by my module, and it
    should be insulated, from other modules and should not be dependent on
    other. That is what a good design says.
    Can you suggest some other solution.

    Gopal
    Lambent Technologies
    Gopal-M, Jun 2, 2004
    #4
  5. Gopal-M

    Dave Moore Guest

    (Gopal-M) wrote in message news:<>...
    > David Harmon <> wrote in message news:<>...
    > > On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++,
    > > (Gopal-M) wrote,
    > > >I have a Base class, say Base and there are many class derived from
    > > >it. At a particular point in my application I need the size of the
    > > >object pointed to by the base pointer. Eg

    > >
    > > You should write your code so that it uses only the declared public
    > > interface of your classes. The size of the object typically is not,
    > > and should not be, anything for foreign code to depend upon.

    >
    > Thanks David and Victor for u r Suggestions. The problem is
    > that i can not demand other module devlopers to implement a getSize()
    > in their module just to return the sizeof their class; which is of no
    > use to their objects and as i am implementing the memory management
    > module, memory management should be handeled by my module, and it
    > should be insulated, from other modules and should not be dependent on
    > other. That is what a good design says.
    > Can you suggest some other solution.
    >
    > Gopal
    > Lambent Technologies


    Hmm .. well, you will probably have to resort to RTTI (which will
    likely be exceedingly ugly) to solve the problem in the way you
    describe. However, I find it hard to believe that there is not a
    better solution on the design end. Actually, I think that making
    getSize() a virtual function of the base class and requiring derived
    classes to implement it may be reasonable in your case, if in fact you
    *really* need to know the sizes of the derived objects to implement
    your memory manager.
    The point is that such knowledge of the size of derived classes is
    generally not necessary in a well-designed application. In most cases
    I am familiar with, only pointers to the base-class are stored .. so
    pointers to the base class are all you need to know the size of. The
    only exception I can think of right now might be by-value copying,
    where you want to copy all of the information in the derived type into
    a new object, but then you are going to have to downcast anyway. Of
    course, once you have done the downcast (correctly) you will be able
    to use sizeof(derived_type) in the subsequent code.

    Basically what I am saying is that without a more clear-cut
    description of your problem, it is hard to suggest any solutions. Why
    don't you give us a streamlined example of real (i.e. compilable and
    tested) code that fails when you don't know the size of the derived
    type?

    Dave Moore
    Dave Moore, Jun 2, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    706
    Ivan Vecerina
    Oct 12, 2003
  2. Alex Vinokur
    Replies:
    7
    Views:
    496
    Clark S. Cox III
    Aug 14, 2006
  3. Replies:
    32
    Views:
    931
    Jerry Coffin
    Aug 18, 2006
  4. Alex Vinokur

    sizeof (size_t) and sizeof (pointer)

    Alex Vinokur, Nov 12, 2007, in forum: C++
    Replies:
    19
    Views:
    785
    Ben Rudiak-Gould
    Nov 30, 2007
  5. cppquester

    sizeof(derived) >= sizeof(base)?

    cppquester, Oct 7, 2011, in forum: C++
    Replies:
    1
    Views:
    304
    Juha Nieminen
    Oct 7, 2011
Loading...

Share This Page