我有这段代码,它给了我分段错误。
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,它可以工作。我不明白为什么。你能帮助我吗?
PS我不能使用字符串。