473,509 Members | 8,693 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(&MyInstance);
}

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

--
Lasse
Jul 19 '05 #1
7 2082
"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(&MyInstance);
}

BTW, this is for a garbagecollector 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.tele.d k...
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<SomeClass>
{
double x;
double y;
};

class SomeOtherClass : public CBL_Object<SomeOtherClass>
{
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
7523
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()...
1
6522
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...
5
2635
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...
6
3277
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
3357
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...
0
1475
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...
7
2535
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...
6
18257
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
4880
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....
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7416
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...
0
7506
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...
0
5656
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,...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
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...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
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...

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.