Skip to main content
GameDev.net gamedev.net
🔒 Locked

Your Thoughts?

Started by mtrneo3 Jun 11, 2002 at 11:45 PM 5 replies 700+ views
Original Post
mtrneo3
mtrneo3
Ok, i''ve been thinking about this lately, and my current debacle with pointers and general reading on memory access has given me an idea... could some form of garbage collecting be acheived by "registering" variables into an array? like tis
 

static int count;

myVariable = new Object;
variable[count] = *myVariable; // the memory location of myVariable
count++;

...

for(i = 0; i 

now i know my syntaxis probably all wrong, but is a system like this feasable? could it be done?

just wondering... thanx    

''''Can I go back?''''
''''No. But if you could, would you really want to?''''

- mr. neo    
                        
''Can I go back?''''No. But if you could, would you really want to?''- mr. neo
Pactuul
Pactuul
first your code is a little incorrect it should be like this:

static int count;
myVariable = new Object;
Object variables[count] = *myVariable;
count++;


for (int i = 0; i < count; i++)
{
if (variables)
{
delete variables[i];
variables[i] = NULL;
}
}


There... that''s better..
Now a problem you could have is if you try to delete myVariable after deleting the array.
so after this if I say:

if (myVariable)
delete myVariable;

then in this case myVariable still points to a memory address, but it''s been deleted and you get an access violation.

There''s smart pointers (which is in the atl) that is a class wrapper for pointers and it keeps a reference count of all pointers accessing this object, so that we solve the problem that I mentioned above.

Hope this gives you some thoughts.
-Pac
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
DerekSaw
DerekSaw
Yes... it can be done. Just overload the new and add some reference count to each newed objects... and then after some interval, in another tread (no harm if it is in the same thread ), try iterating the list and search for any zero-ref-count newed objects, then delete it (and remove from the list too).
"after many years of singularity, i'm still searching n the event horizon" o
Oluseyi
Oluseyi
quote:
Original post by Pactuul
There''s smart pointers (which is in the atl) that is a class wrapper for pointers and it keeps a reference count of all pointers accessing this object, so that we solve the problem that I mentioned above.

Actually, std::auto_ptr is in the STL, and boost::smart_ptr (much more highly regarded) is, obviously, in the Boost library. ATl does have the unique CComPtr and CComQIPtr, though.

mtrneo3:
Using smart pointers is the "standard" solution to your problem. Unlike garbage collection, smart pointer-reference objects/memory get deallocated when the smart pointer object goes out of scope, so you still have control over when "garbage collection" gets done.
mtrneo3
mtrneo3
cool... i was just curious

dereksaw: how would you overload the new without messing up linked librarys and headers and stuff?

''''Can I go back?''''
''''No. But if you could, would you really want to?''''

- mr. neo
''Can I go back?''''No. But if you could, would you really want to?''- mr. neo
George2
George2
I believe there's an article on overloading new/delete operator's at flipcode.


here
it is.


[edited by - George2 on June 12, 2002 3:17:26 PM]
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
DerekSaw
DerekSaw
quote:
Original post by mtrneo3
...how would you overload the new without messing up linked librarys and headers and stuff?...

- mr. neo


Linked libraries are already compiled. Overloading the new won''t effect it. But, if there''s functions (inline?) that defined in include files, the order of #include DOES effect the type of new being referenced to.... unless those function refer to the original new as ::new...


"after many years of singularity, i'm still searching n the event horizon" o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.