473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The size of instances

I'm wondering if there is any way to do run-time checking for the size of
class-instances?
The only idea I've come up with is overloading operator new and saving the
size passes to it on the instance itself.

class CMyClassA
{
public:
int a;
};

class CMyClassA : public CMyClassB
{
public:
int b;
};

void test(CMyClassA *pPtr)
{
// Here I would like to know the size of the instance that pPtr points
to...
}

int main(int argc, char* argv[])
{
CMyClassB MyInstance;
test(&MyInstanc e);
}

BTW, this is for a garbagecollecto r I'm working on just for fun and learning
stuff....

--
Lasse
Jul 19 '05 #1
7 2087
"Lasse Skyum" <no spam> wrote...
I'm wondering if there is any way to do run-time checking for the size of
class-instances?
The only idea I've come up with is overloading operator new and saving the
size passes to it on the instance itself.

class CMyClassA
{
public:
int a;
};

class CMyClassA : public CMyClassB
{
public:
int b;
};

void test(CMyClassA *pPtr)
{
// Here I would like to know the size of the instance that pPtr points
to...
}

int main(int argc, char* argv[])
{
CMyClassB MyInstance;
test(&MyInstanc e);
}

BTW, this is for a garbagecollecto r I'm working on just for fun and learning stuff....


Most of those things (memory managers, garbage collectors (which are
memory managers of sorts)) do store either some "header" information
right in front of the pointer or in a separate table.

Doesn't seem like a language problem, though.

Victor
Jul 19 '05 #2

Most of those things (memory managers, garbage collectors (which are
memory managers of sorts)) do store either some "header" information
right in front of the pointer or in a separate table.


Well yes, I would like to store the data on the instance, but I don't know
where to get the information from, when the object is not created with
operator new?

--
Lasse
Jul 19 '05 #3
"Lasse Skyum" <no spam> wrote in message
news:3f******** *************** @dread16.news.t ele.dk...
Most of those things (memory managers, garbage collectors (which are
memory managers of sorts)) do store either some "header" information
right in front of the pointer or in a separate table.


Well yes, I would like to store the data on the instance, but I don't know
where to get the information from, when the object is not created with
operator new?


There is no standard way to obtain the size of an object created on
the stack. Platform-specific low-level tricks and guesses need to
be used...
Regards, Ivan
--
http://ivan.vecerina.com


Jul 19 '05 #4
"Lasse Skyum" <no spam> wrote:

Most of those things (memory managers, garbage collectors (which are
memory managers of sorts)) do store either some "header" information
right in front of the pointer or in a separate table.


Well yes, I would like to store the data on the instance, but I don't
know where to get the information from, when the object is not created
with operator new?


When it's not created with new, your garbage collector must not delete
it anyway, so that won't be a problem.
Jul 19 '05 #5

Well the reason is, that I'm trying to pair the smart-pointers with the
objects, so I know where the references are comming from. It works great
when I know the size of the instances.

All garbage-collected classes inherit from "CBL_Object " and I was thinking
of something with a template "SizeOf" virtual-function... but I can't quite
figure it out :-(

I was also looking at typeid (RTTI) but it doesn't look like anything I can
use...

--
Lasse
Most of those things (memory managers, garbage collectors (which are
memory managers of sorts)) do store either some "header" information
right in front of the pointer or in a separate table.


Well yes, I would like to store the data on the instance, but I don't
know where to get the information from, when the object is not created
with operator new?


When it's not created with new, your garbage collector must not delete
it anyway, so that won't be a problem.

Jul 19 '05 #6
"Lasse Skyum" <no spam> wrote:

Well the reason is, that I'm trying to pair the smart-pointers with
the objects, so I know where the references are comming from. It works
great when I know the size of the instances.

All garbage-collected classes inherit from "CBL_Object " and I was
thinking of something with a template "SizeOf" virtual-function... but
I can't quite figure it out :-(

I was also looking at typeid (RTTI) but it doesn't look like anything
I can use...


One thing you could try is to make the base class a template and derive
your other classes as template instances, like:
#include <iostream>
#include <cstdlib>

class CBL_Base
{
public:
virtual std::size_t size() const = 0;
};

template <class T>
class CBL_Object : public CBL_Base
{
public:
virtual std::size_t size() const
{
return sizeof(T);
}
//...
};

class SomeClass : public CBL_Object<Some Class>
{
double x;
double y;
};

class SomeOtherClass : public CBL_Object<Some OtherClass>
{
int x;
};

int main()
{
CBL_Base* a = new SomeClass;
CBL_Base* b = new SomeOtherClass;

std::cout << "size of a is " << a->size() << std::endl;
std::cout << "size of b is " << b->size() << std::endl;
}

Jul 19 '05 #7
One thing you could try is to make the base class a template and derive
your other classes as template instances, like:


Thanks Rolf, that was actually a great idea :-)
Jul 19 '05 #8

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

Similar topics

25
7546
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size() might take linear time on some list implementations. That makes sense at first. However, he also says this at the very beginning: "That being the...
1
6529
by: Christian Berg | last post by:
Hi, I have got a problem with resizing the bufferpool of a DB2 v.8.2 instance. The DB2 runs on an AIX 5.x platform. Problem is that an "ALTER BUFFERPOOL ..." command is not persistent if the database is stopped with the db2stop and restarted with the db2start command.
5
2644
by: Mariano Drago | last post by:
Hi there. How can i know how much memory is an instance taking, devided by internal instances. For examples lets say i have 2 classes, C1 and C2. C1 have an internal instance of C2 How can i know the size of: C1 class C2 class C1 instance Another question. If C1 has an instance method (1MB code), and i create 10
6
3284
by: erdem | last post by:
hi all, i guess this question had been asked many times but i couldnt find a way how can we generate a struct with managed code with fixed size i mean struct easy {
17
3367
by: TheMadHatter | last post by:
yello, Quick Q: With regards to ram use, there really isnt any memory savings if I use type bool, vs int....... is there? If I remmeber correctly from a microcontroller course, the smallest addressable unit is an int. All rules still apply for todays machines, right? Thanks in advance.
0
1482
by: Lan W via DBMonster.com | last post by:
I tried to restore DB2 UDB two databases into two different instances on AIX server. the raw devices size of rShortSpace0 and rShortSpace1 on the two instances are the same. # lsvg -l datavg datavg: LV NAME TYPE LPs PPs PVs LV STATE MOUNT POINT db2templv jfs 52 52 1 open/syncd ...
7
2543
by: Curious | last post by:
Hi, I have created my own data structure. Basically it is a two way 'stack', where I can push elements and pop elements both from top and bottom. I also included a counter to show the number of elements currently on the data structure. I need to calculate the size in bytes of this datastructure content at a certain time. What I am...
6
18265
by: Tarun | last post by:
Hi coders, How can check the total RAM size and the number of RAM modules and their respective sizes using C#? Regards, Tarun
43
4896
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy. That is, replace calls to strcpy with say, my_strcpy(dest,src) which will internally find the destination buffer length. For this we need to know...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8336
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...
1
7950
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...
0
8212
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...
0
5389
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...
0
3835
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...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.