How to backtrack a pointer?

backtracking, c, c++, memory, pointers

Solution

No, its not possible to "backtrack" a pointer in C or C++ (a good rule of thumb is if a feature has big hidden performance costs, then its not present in C or C++)

As for the second approach (going through memory looking for pointers), that is precisely what some tools like the Boehm garbage collector do. However, not only is this process inneficient and not portable but it also can lead to "false positives" since you can't tell if a byte pattern in memory is a real pointer or something else like a regular integer or part of a string.

Anyway, you should ask yourself what is the real problem you need to solve instead of trying to hack a garbage collector on your own. Depending on what you want to do there are many ways to approach it in C++ (RAII, smart pointers, etc)

Problem

Let say I have 2 pointers pointing to the same memory location. If I know what the address it is, how can I find out what pointers are pointing to that location? ``` int x=5; int* p1=&x; int* p2=&x; ``` How do I get the address of p1 and p2? Is it possible to even do this in C/C++? If not then is it possible to search through all pointers and see which ones have the value of &x?

Original source