By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

The size of instances

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
"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

P: n/a

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

P: n/a
"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

P: n/a
"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

P: n/a

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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.