Problem with sizeof, when using it with Base class pointer

G

Gopal-M

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
 
V

Victor Bazarov

Gopal-M said:
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
 
D

David Harmon

On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, (e-mail address removed)
(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.
 
G

Gopal-M

David Harmon said:
On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, (e-mail address removed)
(Gopal-M) wrote,

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
 
D

Dave Moore

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top