2010-05-07 46 views
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 *全部轉換爲void *。
GDB輸出:C++:爲什麼一個結構的自指針自動改變到void *

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

不過,我仍然可以看到該結構的數據,如果我隱蔽他們回到PTR *在gdb。
請問這是什麼原因?

我使用的是Arch Linux,GNOME,g ++ 4.5.0,gdb 7.1。沒有任何編譯標誌,但是-g。
This GDB was configured as "i686-pc-linux-gnu"

+0

它在我的系統上顯示'ptr *'。您使用了哪些彙編標誌?什麼版本的g ++和gdb? – Thomas 2010-05-07 07:29:37

+0

奇怪,我也使用gdb 7.1,但是g ++ 4.4.3。 4.5發佈說明並不暗示在這個方向上有任何改變。 http://gcc.gnu.org/gcc-4.5/changes.html – Thomas 2010-05-07 07:59:44

+0

只是爲了填補更多的數據點,使用gdb 7.0.1我得到了正確的答案g ++ 4.2.4和4.3.3,但4.5。 0 gdb將指針顯示爲「void *」。看起來像gcc(或gdb?)中的錯誤 – 2010-05-07 08:57:05

回答

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". 
0

應該指出,「ptr」是/不是一個智能指針,它只是一個結構,甚至沒有析構函數。

(智能指針在C++ land中有非常精確的含義)

+0

嗯,我很抱歉。我不應該在這裏說出一個「聰明的指針」,並對它們有一個模糊的理解。該文本現在已被更正。 – Stone 2010-12-12 04:21:12

+0

沒有傷害。 :) – Arafangion 2010-12-13 01:26:15