C++:为什么结构体的自指针会自动更改为void*?

5
struct ptr{
    int node;
    ptr *next;
    ptr(){}
    ptr(int _node, ptr *_next){ node=_node; next=_next; }
};
struct list_t{
    ptr *sht;
    int size;
    void push(int node){
        size++;
        sht=new ptr(node,sht);
    }
}shthead[100001], comp[200001], tree[200001];

结构 ptr 被用作链表。但是当我在 gdb 中调试代码时,我发现所有的 ptr*'s 都被转换为 void*。
GDB 输出:

(gdb) pt ptr
type = struct ptr {
    int node;
    void *next;
  public:
    ptr(void);
    ptr(int, void *);
}

然而,在gdb中,如果我将它们转换回ptr*,我仍然可以看到结构体的数据。
请问这是什么原因?

我使用Arch Linux、GNOME、g++ 4.5.0和gdb 7.1。没有任何编译标志,只有一个-g。
This GDB was configured as "i686-pc-linux-gnu"


它在我的系统上显示为 ptr *。你使用了哪些编译标志?g++和gdb的版本是什么? - Thomas
奇怪,我也使用gdb 7.1,但是g++ 4.4.3。4.5版本的发布说明没有暗示任何这方面的变化。http://gcc.gnu.org/gcc-4.5/changes.html - Thomas
只是为了填补一些更多的数据点,使用gdb 7.0.1时,我使用g++ 4.2.4和4.3.3得到了正确的答案,但是在4.5.0中,gdb将指针显示为void *。看起来像是gcc(或gdb)的一个错误。 - Mike Dinsdale
3个回答

1

在我的 OS X 上运行良好。

(gdb) pt ptr
type = class ptr {
  public:
    int node;
    ptr *next;

    ptr(void);
    ptr(int, ptr *);
}

gdb 版本:

Shadow:code dkrauss$ gdb -v
GNU gdb 6.3.50-20050815 (Apple version gdb-1346) (Fri Sep 18 20:40:51 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".

1

0

需要提到的是,“ptr”并不是一个智能指针,它只是一个结构体,甚至没有析构函数。

(在C++领域中,“智能指针”有非常精确的含义)


很抱歉,我对此感到非常抱歉。我不应该在对智能指针的模糊理解下在这里说“智能指针”。现在已经更正了文本。 - Stone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接