RTTI?

Discussion in 'C++' started by Rick, Dec 6, 2003.

  1. Rick

    Rick Guest

    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
    Rick, Dec 6, 2003
    #1
    1. Advertising

  2. Rick

    Rolf Magnus Guest

    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.
    Rolf Magnus, Dec 6, 2003
    #2
    1. Advertising

  3. Rick

    Rick Guest

    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
    Rick, Dec 6, 2003
    #3
  4. Rick

    Rick Guest

    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
    Rick, Dec 7, 2003
    #4
  5. Rick

    Rolf Magnus Guest

    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.
    Rolf Magnus, Dec 7, 2003
    #5
  6. Rick

    Chris Dams Guest

    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
    Chris Dams, Dec 7, 2003
    #6
  7. Rick

    Rick Guest

    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
    Rick, Dec 8, 2003
    #7
  8. 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
    Karl Heinz Buchegger, Dec 9, 2003
    #8
  9. Rick

    Rick Guest

    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
    Rick, Dec 9, 2003
    #9
  10. Rick

    tom_usenet Guest

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

    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
    tom_usenet, Dec 9, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Re: RTTI

    John Harrison, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    448
    John Harrison
    Jul 14, 2003
  2. Alf P. Steinbach

    Re: RTTI

    Alf P. Steinbach, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    463
    Alf P. Steinbach
    Jul 14, 2003
  3. Steven Lien

    About RTTI

    Steven Lien, Aug 19, 2003, in forum: C++
    Replies:
    4
    Views:
    594
    Ivan Vecerina
    Aug 19, 2003
  4. BillyO
    Replies:
    2
    Views:
    486
    dslater
    Sep 30, 2003
  5. tirath
    Replies:
    3
    Views:
    705
    Ivan Vecerina
    Oct 12, 2003
Loading...

Share This Page