Reputation: 2451
Is it possible for a function in C++ to find the addresses of all variables in a certain scope? I'm talking about methods such as scanning the memory used by the program, or looking at a compiler's parse tree. Maybe there's even a mechanism added for it in C++11.
This is something I've been wondering about for a few time now, some good answers will be appreciated.
Thanks.
note: the code should be called from inside the program.
Upvotes: 0
Views: 79
Reputation: 67489
This is something that all debuggers can do, so I think it would be possible for a program to get that level of introspection if it is compiled with debug information and can somehow parse its own symbol table.
This project implemented debug info parsing to generate class introspection for C++. I guess the same approach would work for your purposes.
Also, I doubt this will be possible if you compile with optimizations, since the optimizer may change your code enough that a mapping from individual variables in the source code to memory locations does not exist.
Upvotes: 1