2

here is a simple code I am trying to test.

int main()
{
    shared_ptr<int> pa = make_shared<int>(3);
    return 0;
}

And i am using gdb to debug in VScode as followed. ( breakpoint is in return 0 )

-exec p *pa
Could not find operator*.

I think it may happen because the function operator* of shared_ptr is not defined. So I try to call the * in main *pa before return 0;, and it turns out work.

-exec p *pa
$1 = (std::__shared_ptr_access<int, (__gnu_cxx::_Lock_policy)2, false, false>::element_type &) @0x24354c31930: 3

I am wondering if I haven't used *, can I use it during debug. After some search, I try the following code, by instantiating the template in the beginning.

template class shared_ptr<int>;
int main()
{
    shared_ptr<int> pa = make_shared<int>(3);
    return 0;
}

However it still won't work, I guess it may be caused by linker? I am not so sure about compile and link. So how can I use * during debug without use it main, it would be very great if someone can explain it, thanks!

I am using x86_64-w64-mingw32 with gcc version 10.2.0, GNU gdb (GDB) 10.1, and g++ compile with args"-o", "-g", "-Wall", "-Wextra", "-static-libgcc", "-fexec-charset=GBK", "-std=c++11", "-D__USE_MINGW_ANSI_STDIO=1"

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
maoxy
  • 21
  • 2
  • Reading [gcc -O0 still optimizes out "unused" code. Is there a compile flag to change that?](https://stackoverflow.com/questions/39832958/gcc-o0-still-optimizes-out-unused-code-is-there-a-compile-flag-to-change-tha) will help to understand what happens. I would say your question is a duplicate of that link. – 273K Apr 01 '21 at 14:47
  • Is your gdb with enabled python? If yes, try to [register xmethods](https://stackoverflow.com/a/58592592/1983398). – ssbssa Apr 02 '21 at 13:08

0 Answers0