加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如果对象的类类型像A :: B ,如何使用GDB从地址打印c对象成员

发布时间:2020-12-16 05:19:26 所属栏目:百科 来源:网络整理
导读:参见英文答案 Why gdb casting is not working?3 从这个链接 gdb interpret memory address as an object 我们知道,如果A类的对象处于特定的地址,如0x6cf010,那么我们可以使用: (gdb) p *(A *) 0x6cf010 打印此对象的成员元素. 但是,当c命名空间涉及时,这似
参见英文答案 > Why gdb casting is not working?3
从这个链接
gdb interpret memory address as an object
我们知道,如果A类的对象处于特定的地址,如0x6cf010,那么我们可以使用:
(gdb) p *(A *) 0x6cf010

打印此对象的成员元素.

但是,当c命名空间涉及时,这似乎不起作用.也就是说,如果类型为A :: B的对象,则所有以下尝试都不起作用:

(gdb) p *(A::B *) 0x6cf010
(gdb) p *((A::B *) 0x6cf010)

那么,谁知道如何在这个条件下打印对象元素?

我们可以使用以下故意的核心代码尝试从地址打印p的成员(我们可以使用“info locals”显示地址).

#include <stdio.h>

namespace A
{
    class B
    {
    public:
        B(int a) : m_a(a) {}

        void print()
        {
            printf("m_a is %dn",m_a);
        }

    private:
        int  m_a;
    };
}

int main()
{
    A::B *p = new A::B(100);

    p->print();

    int *q = 0;

    // Generating a core here
    *q = 0;
    return 0;

}

解决方法

为我工作:
g++ -g test.cpp -o test
gdb test
(gdb) break main
(gdb) r


Breakpoint 1,main () at test.cpp:22
22      A::B *p = new A::B(100);
(gdb) n
24      p->print();
(gdb) n
m_a is 100
26      int *q = 0;
(gdb) p p
$1 = (A::B *) 0x602010
(gdb) p (A::B *) 0x602010
$2 = (A::B *) 0x602010
(gdb) p *((A::B *) 0x602010)
$3 = {m_a = 100}

它适用于我你使用什么(gcc版本,操作系统,编译标志?)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读