473,769 Members | 3,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
4 3107
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
Jul 22 '05 #2
On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, go***********@y ahoo.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.

Jul 22 '05 #3
David Harmon <so****@netcom. com.invalid> wrote in message news:<41******* *********@news. west.earthlink. net>...
On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, go***********@y ahoo.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
Jul 22 '05 #4
go***********@y ahoo.com (Gopal-M) wrote in message news:<7f******* *************** ****@posting.go ogle.com>...
David Harmon <so****@netcom. com.invalid> wrote in message news:<41******* *********@news. west.earthlink. net>...
On 1 Jun 2004 08:30:28 -0700 in comp.lang.c++, go***********@y ahoo.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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1609
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
2
2469
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
0
3940
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
2
4453
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
39
19647
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
0
2207
by: Slawomir Nasiadka | last post by:
Hi, I'am new to this group so I would like to say "Hello" everyone and here is my problem: I'm writing a simple application (code is at the end of this message) witch would list all mails from a directory from Outlook Express. I have: - OE 6.0 - .NET Framework 1.1 - interface definition language for OE 6.0 - msoeapi.idl
32
2184
by: moleskyca1 | last post by:
This may be stupid question, but why is sizeof(Base) == 1 in: int main(int argc, char* argv) { class Base { }; cout << sizeof(Base) << endl; return 0; }
4
1705
by: Osamede.Zhang | last post by:
Compiler store a pointer table in each object with virtual fuction to implement polymorphic,isn't it? But where is the pointer table? It seems that object only store a pointer to the table,the output of this code is 88. #include<iostream> using namespace std; class MyClass{ public: MyClass(){ a=0;} virtual void tst1() {a-=1;}
5
2539
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to determining the class type. The code is below:
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.