2017-01-02 966 views
5

我有测试程序如下图所示:如何在gdb中查看智能指针的内部数据?

#include<memory> 
#include<iostream> 
using namespace std; 

int main() 
{ 
    shared_ptr<int> si(new int(5)); 
    return 0; 
} 

调试它:

(gdb) l 
1 #include<memory> 
2 #include<iostream> 
3 using namespace std; 
4 
5 int main() 
6 { 
7  shared_ptr<int> si(new int(5)); 
8  return 0; 
9 } 
10 
(gdb) b 8 
Breakpoint 1 at 0x400bba: file testshare.cpp, line 8. 
(gdb) r 
Starting program: /home/x/cpp/x01/a.out 

Breakpoint 1, main() at testshare.cpp:8 
8  return 0; 
(gdb) p si 
$1 = std::shared_ptr (count 1, weak 0) 0x614c20 

,只打印出的si指针类型的信息,但如何获取存储在它的值(这种情况下5)? 如何在调试过程中检查si的内部内容?

+0

你是什么意思与*“在si商店”*? int''si的值指向? –

+0

[如何在GDB中访问std :: tr1 :: shared \ _ptr的目标](https://stackoverflow.com/questions/24917556/how-to-access-target-of-stdtr1shared-ptr-in -gdb)| 'unique_ptr':https://stackoverflow.com/questions/22798601/how-to-debug-c11-code-with-unique-ptr-in-ddd-or-gdb –

回答

1

尝试以下操作:

p *si._M_ptr 

现在,这个假设您正在使用libstdc++.so,给出的输出p si

或者,你可以直接使用值0x614c20(从输出):

p {int}0x614c20 

双方应显示值5

0

但如何获取值存储在它

您将有原始指针转换为存储在std::shared_ptr实际的指针类型。使用whatis来知道实际的指针类型是什么。

(gdb) p si 
$8 = std::shared_ptr (count 1, weak 0) 0x614c20 
(gdb) whatis si 
type = std::shared_ptr<int> 
(gdb) p *(int*)0x614c20 
$9 = 5