作者jeffreyshe (jeffrey)
看板C_and_CPP
標題[問題] struct&string問題
時間Thu Apr 21 06:07:43 2011
最近在寫二元搜尋樹
有一個bug百思不得其解
下面是每個node的struct
typedef struct student
{
unsigned int sid;
unsigned int score;
struct student*llink,*rlink;
string name;
}NodeType;
在insert一個node之後 發現樹的走訪一直有問題
後來發現是name的問題
NodeType student;
student.rlink=NULL;
student.llink=NULL;
cin>>student.name;
cout<<student.name; <--正常
BST.Insert(&student); <--Insert的最後一行也有印出正常的name
cout<<BST.root->score<<BST.root->name;(因為只有一個,就用root去讀了..)
score數值正常,後面就overflow了...測過length()高達2萬多...
把string換成char name[50]就正常了,50是隨意打的,測資的name只有打abc...
不知道問題出在哪裡?
編譯器是dev c++
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.39.182.199
※ 編輯: jeffreyshe 來自: 114.39.182.199 (04/21 06:09)
※ 編輯: jeffreyshe 來自: 114.39.182.199 (04/21 06:18)
→ jeffreyshe:如果寫成name.c_str()也能正確印出來..挺詭異的 04/21 06:21
→ loveme00835:student是區域變數阿!! 04/21 07:56
推 chchwy:推樓上 取一個區域變數的地址 要注意他何時會消滅阿 04/21 09:42
推 kimgtob:好像這次程語的作業 05/02 15:20