Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Problem with sizeof, when using it with Base class pointer (http://www.velocityreviews.com/forums/t283449-problem-with-sizeof-when-using-it-with-base-class-pointer.html)

Gopal-M 06-01-2004 03:30 PM

Problem with sizeof, when using it with Base class pointer
 
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

Victor Bazarov 06-01-2004 04:01 PM

Re: Problem with sizeof, when using it with Base class pointer
 
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

David Harmon 06-01-2004 07:03 PM

Re: Problem with sizeof, when using it with Base class pointer
 
On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, gopal_mca2003@yahoo.com
(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.


Gopal-M 06-02-2004 06:25 AM

Re: Problem with sizeof, when using it with Base class pointer
 
David Harmon <source@netcom.com.invalid> wrote in message news:<4142d256.440367506@news.west.earthlink.net>. ..
> On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, gopal_mca2003@yahoo.com
> (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

Dave Moore 06-02-2004 04:25 PM

Re: Problem with sizeof, when using it with Base class pointer
 
gopal_mca2003@yahoo.com (Gopal-M) wrote in message news:<7ffe85d3.0406012225.72660c28@posting.google. com>...
> David Harmon <source@netcom.com.invalid> wrote in message news:<4142d256.440367506@news.west.earthlink.net>. ..
> > On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, gopal_mca2003@yahoo.com
> > (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


All times are GMT. The time now is 03:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.