我有一個Token
類結構如下:段錯誤:: vector的
class Token {
public:
void* value;
token_type type; // enum to know which type of data is it storing inside value
unsigned int line;
Token() = default;
Token(void* new_value, token_type new_type):
value(new_value), type(new_type)
{}
~Token() {
//free the memory occupied by the object pointed to by value based on it's type
//this also handles the case of the Token being an instantiation of Statement
}
};
然後是類聲明一個聲明,內部令牌知道它持有一個聲明,因爲有一個特定的token_type
類型。內部令牌做載體的清理工作在析構函數,因此必須有一個指向其value
屬性是矢量,但我們也有在聲明該指針的拷貝,所以我們不需要從做演員void*
至std::vector<Token>*
每次;
現在,我試圖做的是這樣的:
std::string* value = new std::string("Sample text");
Token* to_be_pushed = new Token((void*) value, string); //the object pointed to by value will be deleted in this Token's destructor
Statement* new_statement = new Statement;
new_statement->list->push_back(to_be_pushed);
delete new_statement; //Token destructor gets called; It knows it's a statement,
//so it knows value is pointing to a std::vector<Token*> object, and it deletes each pointer in that vector and then the vector itself
的問題,但是,我在哪裏我在new_statement->list
末推to_be_pushed
線得到一個分段錯誤。
我試過把這條線分成兩部分,我知道問題出在我打電話時list->push_back
,而不是訪問new_statement->list
。
這是我從GDB得到了回溯:
#0 0xb6e51410 in memmove()
from /system/lib/libc.so
#1 0x2a0066f8 in std::__copy_move<true, true, std::random_access_iterator_tag>::__copy_m<Token*>
(__first=0x2a0198d0, __last=0x0,
__result=0x2a019908)
at /data/data/com.termux/files/usr/include/bits/stl_algobase.h:378
#2 0x2a006640 in std::__copy_move_a<true, Token**, Token**> (__first=0x2a0198d0, __last=0x0,
__result=0x2a019908)
at /data/data/com.termux/files/usr/include/bits/stl_algobase.h:395
#3 0x2a007070 in std::__copy_move_a2<true, Token**, Token**> (__first=0x2a0198d0, __last=0x0,
__result=0x2a019908)
at /data/data/com.termux/files/usr/include/bits/stl_algobase.h:432
#4 0x2a007010 in std::copy<std::move_iterator<Token**>, Token**> (__first=..., __last=...,
__result=0x2a019908)
at /data/data/com.termux/files/usr/include/bits/stl_algobase.h:464
#5 0x2a006fb0 in std::__uninitialized_copy<true>::__uninit_copy<std::move_iterator<Token**>, Token**> (__first=..., __last=...,
__result=0x2a019908)
at /data/data/com.termux/files/usr/include/bits/stl_uninitialized.h:93
#6 0x2a006f70 in std::uninitialized_copy<std::move_iterator<Token**>, Token**> (__first=...,
__last=..., __result=0x2a019908)
at /data/data/com.termux/files/usr/include/bits/stl_uninitialized.h:123
#7 0x2a006eec in std::__uninitialized_copy_a<std::move_iterator<Token**>, Token**, Token*> (
__first=..., __last=...,
__result=0x2a019908)
at /data/data/com.termux/files/usr/include/bits/stl_uninitialized.h:279
#8 0x2a006dc0 in std::__uninitialized_move_if_noexcept_a<Token**, Token**, std::allocator<Token*> > (__first=0x2a0198d0, __last=0x0,
__result=0x2a019908, __alloc=...)
at /data/data/com.termux/files/usr/include/bits/stl_uninitialized.h:300
#9 0x2a007264 in std::vector<Token*, std::allocator<Token*> >::_M_emplace_back_aux<Token* const&> (this=0x2a019908,
[email protected]: 0x2a0198d0)
at /data/data/com.termux/files/usr/include/bits/vector.tcc:457
#10 0x2a005b70 in std::vector<Token*, std::allocator<Token*> >::push_back (this=0x2a019908,
[email protected]: 0x2a0198d0)
at /data/data/com.termux/files/usr/include/bits/stl_vector.h:1049
這究竟是爲什麼?我究竟做錯了什麼?這是我發佈的錯誤代碼嗎?
'Token((void *)list,statement);'你似乎認爲調用基類的構造函數。它不會:基類使用默認的構造函數進行初始化。相反,它會創建一個臨時未命名的「Token」實例,該實例會立即銷燬。換句話說,這是一個無操作。這進一步加劇了'Token()'默認構造函數將其成員未初始化,包含隨機垃圾的事實。 –
@Igor Tandetnik我怎麼才能實現正確的行爲,即在令牌的'value'和Statement的'list'中存儲與新的'std :: vector'對象相同的指針?然而,指針已經以正確的方式存儲在'list'中,所以我的錯誤與此無關。感謝您指出。 –
user6245072
您還沒有顯示'〜Token()'析構函數,但可能會嘗試訪問其成員變量,這些變量也是未初始化的並且包含隨機垃圾;於是你的程序展現出未定義的行爲。我很確定,我指出的問題實際上與您觀察到的行爲直接相關。 –