如何查看gdb中的智能指针的内部数据? [英] How to view the internal data of a smart pointer inside gdb?

查看:3902
本文介绍了如何查看gdb中的智能指针的内部数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下测试程式:

I've got test program like below:

#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 的内部内容?

It only prints out the pointer type information of si, but how to get the value stored in it (in this case 5)? How can I check the internal content of si during debugging?

推荐答案

请尝试以下操作:

Try the following:

p *si._M_ptr

现在,假设您使用 libstdc ++。so ,给定<$ c $的输出c> p si 。

Now, this assumes that you're using libstdc++.so, given the output for p si.

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

Alternatively, you could use the value 0x614c20 directly (from your output):

p {int}0x614c20

两者都应显示值 5

这篇关于如何查看gdb中的智能指针的内部数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆