RTTI?

R

Rick

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
 
R

Rolf Magnus

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

Rick

Rolf said:
Rick wrote:




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
 
R

Rolf Magnus

Rick said:
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.
 
C

Chris Dams

Hi!

Rick said:
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
 
R

Rick

Chris said:
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
 
K

Karl Heinz Buchegger

Rick said:
Chris said:
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/
 
R

Rick

Karl said:
Rick said:
Chris said:
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
 
T

tom_usenet

gc_ptr is presumably a template?

I was
So doGC is a static method?

What is myRuntime?

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();
}
}

You can easily register each type with myRuntime when it is
instantiated.

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
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top