我有這段代碼,它給我分段錯誤。const在結構類中的字符char - 分段錯誤
struct TRecord {
const char * Id;
const char * Name;
};
class CClass {
CClass();
~CClass();
bool Add (const char * id, const char * name);
TRecord ** m_record;
int m_count;
};
CClass::CClass (void) {
m_count = 0;
m_record = new TRecord * [1000];
}
CClass::~CClass(void) {
for(int i=0;i<m_count;i++){
delete m_record[i];
}
delete [] m_record;
}
bool CClass::Add (const char * id, const char * name) {
m_record[m_count] -> Id = new char[11];
m_record[m_count] -> Name = new char[strlen(name) + 1];
m_record[m_count] -> Id = id;
m_record[m_count] -> Name = name;
m_count++;
return true;
}
如果我添加例如const char haha [2222];結構TRecord,它的工作原理。我不明白爲什麼。你可以幫我嗎?
P.S.我不能使用字符串。
哪種方法可以調用哪個對象來產生分段錯誤? – Oswald 2013-04-09 20:37:00
是一個完整的小例子太多要問? – Beta 2013-04-09 20:37:47
這條線是無效的C++:'m_record = new TRecord * [1000];' – 2013-04-09 20:42:44