Thread: How many pointer are pointing to this object

  1. #1
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46

    How many pointer are pointing to this object

    Dear all,
    Now I want to implement reference counter in C, for automatic garbage collection.
    For example, now I have a struct x.
    How to know, how many are there pointer are pointing to this struct? If I know, and the number of pointer is 0, I can free this struct.
    Please give me an idea.
    Any help are welcome.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You won't know "automagically"... you're going to have to write your own version of malloc() that keeps track of it for you.

  3. #3
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear Tater,
    Thanks for your help
    But, let consider the following example

    my_struct struct1;
    my_struct* pointer;
    pointer = &struct1;
    pointer = &struct1;
    So, in this case, how many pointers are pointing to struct1? 1 or 2 pointers?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hannibal2010
    So, in this case, how many pointers are pointing to struct1? 1 or 2 pointers?
    Considering that there is only one pointer in your code snippet...
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Yes, I know
    But how can I know it in source code.
    I mean, there are 2 times a pointer point to my_struct, so how can I know these 2 pointers are the same, and reference count is still only 1.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am far from an expert on the implementation of garbage collectors, but perhaps examining the Boehm-Demers-Weiser garbage collector would be a start.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Have a look at the C99 qualifier restrict.
    This is a good hint for any GC contexts.

  8. #8
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear laserlight & BillyTkid
    Thanks for your help
    I will check

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer pointing to itself
    By rrahulvverma in forum C Programming
    Replies: 20
    Last Post: 07-19-2010, 09:10 AM
  2. pointer to struct object in class object
    By Stevo in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 07:58 PM
  3. function pointer not pointing
    By darfader in forum C Programming
    Replies: 3
    Last Post: 09-17-2003, 02:31 AM
  4. Pointing a function pointer to a variable?
    By Aidman in forum C++ Programming
    Replies: 9
    Last Post: 07-06-2003, 10:50 PM
  5. pointer which is pointing to new~
    By black in forum C++ Programming
    Replies: 12
    Last Post: 05-10-2002, 08:31 AM