Connecting Tech Pros Worldwide Help | Site Map

RTTI?

Rick
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 22 '05

re: RTTI?


Rick wrote:
[color=blue]
> 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[/color]

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

Rick
Guest
 
Posts: n/a
#3: Jul 22 '05

re: RTTI?




Rolf Magnus wrote:
[color=blue]
> Rick wrote:
>
>[color=green]
>>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[/color]
>
>
> No. RTTI is limited to polymorphic classes. No classes without virtual
> member functions and no builtin types.[/color]

Then is it possible at all to do that?

Rick

Rick
Guest
 
Posts: n/a
#4: Jul 22 '05

re: RTTI?




Rick wrote:[color=blue]
>
>
> Rolf Magnus wrote:
>[color=green]
>> Rick wrote:
>>
>>[color=darkred]
>>> 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[/color]
>>
>>
>>
>> No. RTTI is limited to polymorphic classes. No classes without virtual
>> member functions and no builtin types.[/color]
>
>
> Then is it possible at all to do that?
>
> Rick
>[/color]

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

Regards,

Rick

Rolf Magnus
Guest
 
Posts: n/a
#5: Jul 22 '05

re: RTTI?


Rick wrote:
[color=blue]
>
>
> Rick wrote:[color=green]
>>
>>
>> Rolf Magnus wrote:
>>[color=darkred]
>>> 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.[/color]
>>
>>
>> Then is it possible at all to do that?
>>
>> Rick
>>[/color]
>
> Can someone please let me know... I'm still stuck with it.[/color]

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.

Chris Dams
Guest
 
Posts: n/a
#6: Jul 22 '05

re: RTTI?


Hi!

Rick <rrquick@nospam-com> writes:
[color=blue][color=green][color=darkred]
>>> 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.[/color]
>>
>> Then is it possible at all to do that?
>>[/color]
>Can someone please let me know... I'm still stuck with it.[/color]

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
Rick
Guest
 
Posts: n/a
#7: Jul 22 '05

re: RTTI?




Chris Dams wrote:[color=blue]
> 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.[/color]

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

Karl Heinz Buchegger
Guest
 
Posts: n/a
#8: Jul 22 '05

re: RTTI?


Rick wrote:[color=blue]
>
> Chris Dams wrote:[color=green]
> > 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.[/color]
>
> 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.[/color]

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
kbuchegg@gascad.at
Rick
Guest
 
Posts: n/a
#9: Jul 22 '05

re: RTTI?




Karl Heinz Buchegger wrote:[color=blue]
> Rick wrote:
>[color=green]
>>Chris Dams wrote:
>>[color=darkred]
>>>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.[/color]
>>
>>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.[/color]
>
>
> 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/
>[/color]

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

tom_usenet
Guest
 
Posts: n/a
#10: Jul 22 '05

re: RTTI?


On Sun, 07 Dec 2003 15:10:01 +1100, Rick <rrquick@nospam-com> wrote:
[color=blue][color=green][color=darkred]
>>>> 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.[/color][/color][/color]

gc_ptr is presumably a template?

I was[color=blue][color=green][color=darkred]
>>>> 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();[/color][/color][/color]

So doGC is a static method?
[color=blue][color=green][color=darkred]
>>>> for doubles and ints. I want a simple call such as:
>>>>
>>>> myRuntime::doGC();[/color][/color][/color]

What is myRuntime?
[color=blue][color=green][color=darkred]
>>>>
>>>> Is this possible via RTTI? Thanks[/color][/color][/color]

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.
[color=blue][color=green][color=darkred]
>>> No. RTTI is limited to polymorphic classes. No classes without virtual
>>> member functions and no builtin types.[/color]
>>
>>
>> Then is it possible at all to do that?[/color][/color]

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
Closed Thread


Similar C / C++ bytes