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

查看:32
本文介绍了如何查看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的内部内容?

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?

推荐答案

试试以下:

p *si._M_ptr

现在,假设您正在使用 libstdc++.so,给出 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天全站免登陆