可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
From this code:
int x = 5;
int other = 10;
vector<int*> v_ptr;
v_ptr.push_back(&x);
v_ptr.push_back(&other);
v_ptr.push_back(&x);
Is there anyway I can know who points at x
, from the x
variable itself, so that I don't have to search inside v_ptr
for address of x
? Is this possible in C++ or C++0x?
I read that when doing garbage collection, they look at memory and see if anything points at it or not, then make decisions to delete the unused variable, etc.
回答1:
No. It is like asking a person if they know everyone who knows their address.
回答2:
No, you can not know what has a reference to x without iterating through the possible places you assigned it(v_ptr
)
--Or--
If you must do this, you may want to do some kinda reference tracking(which can be used for garbage collection) like
v_ptr[0]=add_reference(&x,&v_ptr[0]);
where add_reference is some function to have a list of references made to the first argument, with the referrer as the second argument(which may be tricky with STL types)
回答3:
No it is not possible to know who points at x.
In addition C++ is not garbage collected.
Even if you use shared_pointer's to x, you can find out how many pointers there are to x, but not whom they are.
回答4:
No, it is not possible to know this with a raw pointer.
Certain types of "smart pointers" (which are actually objects that contain pointers plus other meta-data about the pointer) keep as part of their meta-data a list or count of all references to the pointed-to object from other smart pointers. In garbage collected languages, this mechanism is used to determine if an object is no longer referenced, but it is not a characteristic of a standard C or C++ pointer.
回答5:
In addition to the other accurate answers here, note that in garbage collected languages (C++.NET, I guess, or any of the normal other ones, Java/C#, etc), one technique for garbage collection is to traverse references, marking everything that is pointed to.
But note that this actually works the other direction. I start from a known set of objects, and follow all of their links to other objects, etc. I generally never am able to say "given this object, let me calculate who points to it or holds references to it".
回答6:
The answer to your actual question is no, C++ doesn't have a mechanism for figuring out how many references are still active. Nonetheless,while C++ is not garbage collected, if you're interested, you can try one of the gc_classes. Here's a stackoverflow post listing some of them: Garbage collection Libraries in C++
回答7:
Yes, you can know if--at a given point in execution--there is a pointer to your variable. All you need to do is keep track of the memory allocated to every variable in the process. This means knowing the start and end addresses of the stack and the heap. You can then do a simple sequential search for the location of your variable in those address ranges.
Though iterating over those relatively small portions of memory should not take long, you could optimize the search time at the expense of some additional memory overhead and pointer creation overhead by maintaining a structure that tracks only references. That gives you a smaller list to search.