I need to find name of varible by address in c++. I have no idea about it for what code can i use ? Tell me how to fix this problem or is there any library in c++
Asked
Active
Viewed 65 times
-2
-
2Simple answer - this is not possible. – Diodacus Apr 25 '19 at 07:44
-
2(Slightly longer answer:) C++ is not a reflective language (like e.g. C#). At run-time, variable names are not available. The only exception is debug info which provides a mapping from addresses to symbols (incl. variable names) but that's an optional feature and compiler dependent. Beside of this, if reflection is needed (to a certain extent), it has to be implemented by custom (C++) code. – Scheff's Cat Apr 25 '19 at 07:45
-
3Please explain in details the initial problem you are trying to solve. Maybe it has another simpler solution. – vahancho Apr 25 '19 at 07:47
-
This might be of interest: [SO: How can I add reflection to a C++ application?](https://stackoverflow.com/q/41453/7478597). This and more can be found by [google "stackoverflow c++ reflection"](https://www.google.com/search?q=stackoverflow+c%2B%2B+reflection) (to give you a start). – Scheff's Cat Apr 25 '19 at 07:49
-
Most likely a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) (-> @vahancho). – Aconcagua Apr 25 '19 at 07:54
-
Let u have adresses in which some binary is placed now the problem is we do not know that is it character type ,float type ,integer type or string type ?? – Mahar Ali Apr 25 '19 at 07:54
-
Where does that data come from? You know, apart from alignment issues, *any* data could reside at *any* address. So if you don't have additional information, no chance. – Aconcagua Apr 25 '19 at 07:55
-
Doesn't this problem exist only if you reverse-engineer binary code? (Only) one of the intentions of compiled code is the saving of intellectual properties... ;-) – Scheff's Cat Apr 25 '19 at 07:55
-
@MaharAli Variable names in c++ are just hollow words. They won't be kept in the binaries (unless for debugging purposes). – πάντα ῥεῖ Apr 25 '19 at 07:57
-
If it's really about de-compiling (-> @Scheff), then addresses won't serve you *anything at all*. You need to look at what the code in question *does* with this address (e. g. load value from memory address and add it on FPU stack -> data must be float or double). – Aconcagua Apr 25 '19 at 07:58
-
@Aconcagua The intention of OP is still not clear. Otherwise, may be, I had mentioned [IDA](https://www.hex-rays.com/products/ida/index.shtml) as well... ;-) – Scheff's Cat Apr 25 '19 at 08:00
-
@Scheff Hm, suppose I haven't made clear enough that it only was intended as example (but catching up your comment); should (only) enlighten a bit more that addresses on their own are useless... Almost at least (odd address most likely won't be `int`...). – Aconcagua Apr 25 '19 at 08:03
1 Answers
1
If you mean to find the string that specifies the name of some arbitrary variable(class member or local) by the memory address at runtime - the answer is no for several reasons:
- In the binary without symbols there is just no information about the original names of variables and classes.
- I guess in a run-time library implementation objects do not contain any extra information to find that this address corresponds to a certain object in the runtime. For objects of classes without virtual table you even can't know the actual type in runtime.
If the question is about more specific situation - when you have an address that corresponds to an object of the known base address of a certain type - yes it could be implemented by comparing the address with pointer to members. Example:
struct Foo
{
int Member1;
double Member2;
};
enum class Member
{
Member1,
Member2,
None
};
Member getMember(const Foo* baseAddress, void* address)
{
if (address == &(baseAddress->Member1))
{
return Member::Member1;
}
else if (address == &(baseAddress->Member2))
{
return Member::Member2;
}
else
{
return Member::None;
}
}

Dmitry Gordon
- 2,229
- 12
- 20