The size of instances

L

Lasse Skyum

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....
 
V

Victor Bazarov

Lasse Skyum said:
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
 
L

Lasse Skyum

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?
 
I

Ivan Vecerina

Lasse Skyum said:
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
 
L

Lasse Skyum

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...
 
R

Rolf Magnus

Lasse Skyum said:
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.
 
R

Rolf Magnus

Lasse Skyum said:
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;
}
 
L

Lasse Skyum

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 :)
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top