Reputation: 105
I want to know which pointers point to a specific object at runtime in c/c++.
For example, I have an object A, now I want to do something for A, such as changing the memory address of A, at this moment I have to know there are how many pointer pointing to A, and,at least, the name of these pointers at runtime.
In fact I just want to realize the migration of an object or some data else, without making the program wrong. Because in c/c++ if I want to migrate an object A I must figure out how many pointer pointing to the object A, and than make these pointer pointing to the new address which is the address of the object A after migration.
Totally speaking, it likes the std::shared_ptr, and I also have realized a self-defined class to achieve this goal. But now, I just want to know are there some ways to achieve this goal by using some compiler tools such as LLVM, and without modifying the source code or modifying the source automatically.
*******problem description**********
In fact, I have two types of memory, typeA and typeB, and now I want to realize a function which can migrate an object X in typeA to typeB. To make sure the validity of programs, after migrating object X form typeA to typeB,I have to know how many pointers point to the object X and than make these pointers pointing to the new address of object X.
Upvotes: 0
Views: 924
Reputation: 1
I want to know which pointers point to a specific object at runtime in c/c++.
In general this is not possible. BTW, it looks that you want to implement something quite similar to a precise copying garbage collector. If this is not clear to you, read the GC handbook (for the concepts and the terminology). Notice that variables (and their names) don't exist at runtime (only memory locations, perhaps on the call stack, are relevant then) but only at compile time.
Totally speaking, it likes the
std::shared_ptr
, and I also have realized a self-defined class to achieve this goal. But now, I just want to know are there some ways to achieve this goal by using some compiler tools such as LLVM, and without modifying the source code or modifying the source automatically.
Again, in general that is not possible. For an intuition about why, imagine that you deal with a union { Foo* fooptr; std::uintptr foonum; };
which is in fact discriminated by some arbitrary external property, like e.g. the oddness of your current pid. If you could solve that you have solved the halting problem. Read also about Rice's theorem. Both are very relevant for static source code analysis.
However, what you could do is define explicitly some additional strong coding rules and constraints, (perhaps with some support runtime library) and design and implement, e.g. with LLVM or some GCC plugin, a static analyzer which check against these coding rules and constraints.
Notice that in practice you can use some GCs with C++, e.g. Boehm's GC or
Ravenbrook's MPS.
Your question is still lacking a real motivation (which I am trying to guess) and looks like some XY problem. "Like a GC" is not enough.
See also this. Perhaps read about dynamic software updating or application checkpointing.
your problem (even with the problem description edit, I don't understand it well) looks similar to copying garbage collection techniques like Cheney's algorithm.
Upvotes: 2
Reputation: 273
For this purpose, you can create a class which record amount of reference and every reference name. Like this
class custom_obj {
Custom A; // target object
int count_ptr; // reference count
vector<void*> ref_ptr; // reference pointer
};
That's a simple way.
Upvotes: 0