473,466 Members | 4,855 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

RTTI?

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
9 2031
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


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


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


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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
by: Kleidemos | last post by:
If I implement a simple RTTI system, more simple than C++ RTTI system for my program and this system is plus or minus: #define DEF_RTTI_BASE(name) virtual inline const char *Name(){ return...
9
by: Agoston Bejo | last post by:
Hello there, I would like to know what overheads there are to think of when using RTTI. I understand that enabling RTTI increases the sizes of the classes, but not the objects themselves. This...
2
by: denny | last post by:
Hey all, I know that dynamic_cast<> takes some time, but , for instance, is there a memoy cost associated in with it? Does it have to maintain a table in memory, thus bloating the runtime ram...
3
by: Steven T. Hatton | last post by:
I'm trying to work out a design for dynamically determining file types, and for generating new files of a given type. I'm curious to know what others think of my current strategy. Is it "so...
10
by: Neo | last post by:
what is RTTI? and how to implements it? give me sourceful link for learn and implements it (using standard C mean portable). regards, -aims
3
by: Neo | last post by:
Hi Friends, I have a question about RTTI. 1) In C++ we have vptr pointing to vtables which helps to make use of pointer as polymorphic entities. 2) Without knowing object type I can call methods...
5
by: dotNeter | last post by:
I'm studying the RTTI, and my current work is concern for how to get the self-defined type at runtime, that's exactly what the RTTI does. I mean, in my application, I built several self-defined...
2
by: Chameleon | last post by:
I know than dynamic_cast check string name of derived to base class and if one of them match, return the pointer of that object or else zero. I suppose, I dynamic_cast instead of strings, checks...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.