9

We have a C++ program such as:

int&
Instance()
{
    static int test;
    return test;
}

int
main( int argc, char ** argv )
{
    int& test = Instance();
    printf("%d\n",test);
    
    return 0;
}

Now I'm trying to print Instance::test, as suggested by some gdb documentation: https://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_52.html

However, this yields:

(gdb) p Instance::test

No symbol "test" in specified context.

I'm compiling with -O0 -g.

3
  • I'm wondering whether it's undefined behavior to pass references to a function printf that knows nothing about what a reference is. It just seems wrong to me. I'll stick my neck out -- I don't have the C++ spec in front of me right now, but I'm hedging my bet that the code has undefined behavior due to passing a reference to printf() Commented Aug 12, 2014 at 21:30
  • Why wouldn't it be fine? I'm using a reference which will result in a copy of an integer. The only issue that I could see is with the vararg implementation. In my experience and this example, it works fine. But this is also not the question at hand Commented Aug 14, 2014 at 2:52
  • 1
    The only issue that I could see is with the vararg implementation Which was my point. Plus, comments are meant for comments, that's I didn't post my comment as an answer. Commented Aug 14, 2014 at 3:00

1 Answer 1

16
(gdb) p 'Instance()::test'

works for me on Ubuntu 14.04, gdb 7.7, g++ 4.8.2 . How did I know that was the symbol?

$ nm -C a.out | grep test
0000000000601040 d Instance()::test
Sign up to request clarification or add additional context in comments.

5 Comments

Correct. The key is the single quotes.
@Sioux Yes - I should have called attention to that from the beginning.
I was having a similar problem, and your nm trick solved it for me. However, the symbol was prevRev.6906. It worked, but what is this symbol, and how was I supposed to guess it?
@baruch what compiler, version, and flags are you using? And are you compiling the code in the original question, or other code?
Other code. gcc (mingw64) 4.9.2, -g. In my code, the static variable scope is not directly in a function, but rather in an inner block (it is declared within an if block)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.