【问题标题】:C++: why a self pointer of a struct automatically changes to void*C++:为什么结构的自指针会自动更改为 void*
【发布时间】:2010-05-07 07:24:18
【问题描述】:
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];

struct ptr 用作链表。但是当我在 gdb 中调试代码时,我发现 ptr* 全部转换为 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是什么版本的?
  • 奇怪,我也用gdb 7.1,但是g++ 4.4.3。 4.5 发行说明没有暗示这个方向的任何变化。 gcc.gnu.org/gcc-4.5/changes.html
  • 只是为了填写更多数据点,使用 gdb 7.0.1 我得到了 g++ 4.2.4 和 4.3.3 的正确答案,但使用 4.5.0 gdb 将指针显示为 void* .看起来像 gcc(或 gdb?)中的错误

标签: c++ pointers struct gdb


【解决方案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".

【讨论】:

    【解决方案2】:

    也许是这个:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45088

    我要感谢 Tom Tromey 告诉我这些。

    【讨论】:

      【解决方案3】:

      值得一提的是,“ptr”是/not/智能指针,它只是一个结构,甚至没有析构函数。

      (智能指针在 C++ 领域具有非常精确的含义)

      【讨论】:

      • 好吧,我很抱歉。我不应该在这里说“智能指针”,而对它们有模糊的理解。文字现已更正。
      猜你喜欢
      • 2016-11-10
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多