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

RTTI?

P: n/a
Hi,

I wrote a few classes recently and am writing a small GC implementation
for C++. I will be implementing my own gc_ptr type maintain a list where
I'll store pointers to allocated memory. I was wondering if it is
possible to identify objects during runtime via RTTI because I don't
want to be doing:

gc_ptr<int>::doGC();
gc_ptr<double>::doGC();

for doubles and ints. I want a simple call such as:

myRuntime::doGC();

Is this possible via RTTI? Thanks
Rick

Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Rick wrote:
Hi,

I wrote a few classes recently and am writing a small GC
implementation for C++. I will be implementing my own gc_ptr type
maintain a list where I'll store pointers to allocated memory. I was
wondering if it is possible to identify objects during runtime via
RTTI because I don't want to be doing:

gc_ptr<int>::doGC();
gc_ptr<double>::doGC();

for doubles and ints. I want a simple call such as:

myRuntime::doGC();

Is this possible via RTTI? Thanks


No. RTTI is limited to polymorphic classes. No classes without virtual
member functions and no builtin types.

Jul 22 '05 #2

P: n/a


Rolf Magnus wrote:
Rick wrote:

Hi,

I wrote a few classes recently and am writing a small GC
implementation for C++. I will be implementing my own gc_ptr type
maintain a list where I'll store pointers to allocated memory. I was
wondering if it is possible to identify objects during runtime via
RTTI because I don't want to be doing:

gc_ptr<int>::doGC();
gc_ptr<double>::doGC();

for doubles and ints. I want a simple call such as:

myRuntime::doGC();

Is this possible via RTTI? Thanks

No. RTTI is limited to polymorphic classes. No classes without virtual
member functions and no builtin types.


Then is it possible at all to do that?

Rick

Jul 22 '05 #3

P: n/a


Rick wrote:


Rolf Magnus wrote:
Rick wrote:

Hi,

I wrote a few classes recently and am writing a small GC
implementation for C++. I will be implementing my own gc_ptr type
maintain a list where I'll store pointers to allocated memory. I was
wondering if it is possible to identify objects during runtime via
RTTI because I don't want to be doing:

gc_ptr<int>::doGC();
gc_ptr<double>::doGC();

for doubles and ints. I want a simple call such as:

myRuntime::doGC();

Is this possible via RTTI? Thanks


No. RTTI is limited to polymorphic classes. No classes without virtual
member functions and no builtin types.

Then is it possible at all to do that?

Rick


Can someone please let me know... I'm still stuck with it.

Regards,

Rick

Jul 22 '05 #4

P: n/a
Rick wrote:


Rick wrote:


Rolf Magnus wrote:
Rick wrote:
Hi,

I wrote a few classes recently and am writing a small GC
implementation for C++. I will be implementing my own gc_ptr type
maintain a list where I'll store pointers to allocated memory. I
was wondering if it is possible to identify objects during runtime
via RTTI because I don't want to be doing:

gc_ptr<int>::doGC();
gc_ptr<double>::doGC();

for doubles and ints. I want a simple call such as:

myRuntime::doGC();

Is this possible via RTTI? Thanks

No. RTTI is limited to polymorphic classes. No classes without
virtual member functions and no builtin types.

Then is it possible at all to do that?

Rick


Can someone please let me know... I'm still stuck with it.


Well, anything can be done somehow. If you want to know the type of
every object that was dynamically allocated with your GC, you will have
to maintain that information yourself somehow.

Jul 22 '05 #5

P: n/a
Hi!

Rick <rrquick@nospam-com> writes:
Rick wrote:

I wrote a few classes recently and am writing a small GC
implementation for C++. I will be implementing my own gc_ptr type
maintain a list where I'll store pointers to allocated memory.


Then is it possible at all to do that?

Can someone please let me know... I'm still stuck with it.


Well, to begin with, why do you need garbarge collection for things like
ints and doubles anyway? Even java does not garbage collect these types
normally. It is not like these types take that much memory that you can't
afford to copy them and therefore need multiple pointers to them. If you
really need, you could try to make all your gc_ptr types inherit from a
single ptr_daddy that is not a template. In your list, you then store
pointers to ptr_daddy's. You need a virtual function inside your ptr_daddy
to be able to call the correct delete operator for the different types
your gc_ptr's point to.

Have a nice day,
Chris Dams
Jul 22 '05 #6

P: n/a


Chris Dams wrote:
Well, to begin with, why do you need garbarge collection for things like
ints and doubles anyway? Even java does not garbage collect these types
normally.


Because something like:

int *a = new int[10000];

will cause a memory leak if I don't collect it once a is modified to:

int b = 99;
a = &b;

Java's primitive types are stored on the stack *but* something like:

int[] a = new int[1000];

in java would store the array on the heap, which *is* garbage collected
eventually.
This is why I have a garbage collector to do the dirty work for me.
Rick

Jul 22 '05 #7

P: n/a
Rick wrote:

Chris Dams wrote:
Well, to begin with, why do you need garbarge collection for things like
ints and doubles anyway? Even java does not garbage collect these types
normally.


Because something like:

int *a = new int[10000];

will cause a memory leak if I don't collect it once a is modified to:

int b = 99;
a = &b;

Java's primitive types are stored on the stack *but* something like:

int[] a = new int[1000];

in java would store the array on the heap, which *is* garbage collected
eventually.

This is why I have a garbage collector to do the dirty work for me.


C++ is programmed differently then Java.
In C++ you would write:

int a[1000];

and be done with it. No need to allocate dynamically.
If you must allocate dynamically, you can always use:

#include <vector>

...
int some_number;
...
std::vector< int > a( some_number );

which allocates a dynamic array (the vector) of a specific size and
does all the dirty work of managing the memory for you (a vector
can even grow as necessary).

Please try to learn C++ and the way things are done in C++ instead
of applying Java techniques (and wasting time by rebuilding them)
to C++. In C++ there is no need for a GC, the RAII idiom is
usually the way to go and works well. But if you still feel the need
for a GC, then check out

http://www.hpl.hp.com/personal/Hans_Boehm/gc/

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

P: n/a


Karl Heinz Buchegger wrote:
Rick wrote:
Chris Dams wrote:
Well, to begin with, why do you need garbarge collection for things like
ints and doubles anyway? Even java does not garbage collect these types
normally.


Because something like:

int *a = new int[10000];

will cause a memory leak if I don't collect it once a is modified to:

int b = 99;
a = &b;

Java's primitive types are stored on the stack *but* something like:

int[] a = new int[1000];

in java would store the array on the heap, which *is* garbage collected
eventually.

This is why I have a garbage collector to do the dirty work for me.

C++ is programmed differently then Java.
In C++ you would write:

int a[1000];

and be done with it. No need to allocate dynamically.
If you must allocate dynamically, you can always use:

#include <vector>

...
int some_number;
...
std::vector< int > a( some_number );

which allocates a dynamic array (the vector) of a specific size and
does all the dirty work of managing the memory for you (a vector
can even grow as necessary).

Please try to learn C++ and the way things are done in C++ instead
of applying Java techniques (and wasting time by rebuilding them)
to C++. In C++ there is no need for a GC, the RAII idiom is
usually the way to go and works well. But if you still feel the need
for a GC, then check out

http://www.hpl.hp.com/personal/Hans_Boehm/gc/


Yes I know all about GCs and I've seen this website. I'm only interested
in building one because I'm interested in learning how to. I dont even
think I'll use it with my programs but writing such things always makes
you learn new stuff which I'm eager to.

Rick

Jul 22 '05 #9

P: n/a
On Sun, 07 Dec 2003 15:10:01 +1100, Rick <rrquick@nospam-com> wrote:
I wrote a few classes recently and am writing a small GC
implementation for C++. I will be implementing my own gc_ptr type
maintain a list where I'll store pointers to allocated memory.
gc_ptr is presumably a template?

I was wondering if it is possible to identify objects during runtime via
RTTI because I don't want to be doing:

gc_ptr<int>::doGC();
gc_ptr<double>::doGC();
So doGC is a static method?
for doubles and ints. I want a simple call such as:

myRuntime::doGC();
What is myRuntime?

Is this possible via RTTI? Thanks
To write a function called doGC? Yes, of course. What do you want it
to do though? Are you asking whether you can recover the type of a
void* pointer? No is the answer. Are you asking something else? I
don't see why you need RTTI at all yet. e.g.

void myRuntime::doGC()
{
for (int i = 0; i < numRegisteredGCTypes; ++i)
{
gcTypeDoGC[i]();
}
}

You can easily register each type with myRuntime when it is
instantiated.
No. RTTI is limited to polymorphic classes. No classes without virtual
member functions and no builtin types.

Then is it possible at all to do that?


Do what? You haven't asked a clear question about why you need RTTI.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.