0

I'm debugging using a pointer pointing to double variables to modify their values. And I want to print the variable name pointed by the pointer every time it changes the variable it points to. This could be by coding for each variable but there's dozens of them and it will take a lot of time. I found this way here at cplusplus.com (the second replies):

#define SHOW(a) std::cout << #a << ": " << (a) << std::endl

But it only prints the pointer's name not the variable it points to.

I use the keyboard to modify the values constantly. It's impossible to use the debugger.

Dlean Jeans
  • 966
  • 1
  • 8
  • 23
  • 1
    That's because memory doesn't contain any variable names. You'll have to use symbols and debugging information provided by your compiler, or build your own system for storing this information. There are no variables beyond the compilation phase. – user35443 Jul 12 '15 at 11:09
  • A pointer points to an address, not to a name, it doesn't know and it doesn't matter what the name of that variable on that address is. – Hatted Rooster Jul 12 '15 at 11:10
  • 2
    Probably a better idea is to learn how to use the debugger – Ed Heal Jul 12 '15 at 11:12
  • Think you need to dereference a. << (*a) << std::endl – Steve Kidd Jul 12 '15 at 11:13
  • @Steve Kidd that's not going to give a proper answer, unless the compiler made a smart optimization that associated dereferenced pointer with a specific variable. – user35443 Jul 12 '15 at 11:15
  • But I use the keyboard to modify the values constantly. It's impossible to use the debugger. – Dlean Jeans Jul 12 '15 at 11:17
  • If the macro is passed the variable name (which it is), then that name IS printable. Macro expansion results in the variable name existing within the code. I think the above code will work if he does #define SHOW(a) std::cout << #a << ": " << *(a) << std::endl. No access to compiler at the mo so can't confirm as a answer. – Steve Kidd Jul 12 '15 at 11:21
  • Notwithstanding that using macros is dislike/hated; practicalities mean that code like this is often used in some debugging situations. Some scenarios are best attacked within a debugger. Some scenarios are best attacked by writing log files. It is unclear to me why people are downvoting this question. – Steve Kidd Jul 12 '15 at 11:30
  • @SteveKidd Yeah! Thank you! – Dlean Jeans Jul 12 '15 at 11:39

1 Answers1

1

you can have some global string which will get the current pointee name. then use some macro to do the assigment both for the memory address and both for the name.

example:

#include <iostream>

std::string str;
#define pointTo(x,y) do{ str = #y; x=&y;   }while(0)

int main()
{

    int* x , hiThere ,helloWorld;
    pointTo(x,hiThere);
    std::cout<<str<<"\n";
    pointTo(x,helloWorld);
    std::cout<<str<<"\n";    
}

working example : http://coliru.stacked-crooked.com/a/aaf2ba8ce791d591

but although it works, I highly advise you to ditch this idea. use a debugger instead.

David Haim
  • 25,446
  • 3
  • 44
  • 78