我在介绍性的C++课程中,我的程序在我的复制函数中进行了细分。当我使用GDB时,它说它无法访问地址0x0处的char *。奇怪的是我已经多次写过这个函数,没有任何错误。Segfault尝试strcpy char *
class question
{
public:
question();
~question();
int set_question(char * question);
int copy_question(question & to_copy);
int clear_question();
int display();
char* retrieve_question();
char* retrieve_answer();
private:
char* your_question;
char* correct_answer;
};
///////////////////
int question::set_question(char * question)
{
your_question = new char [strlen(question)+1];
strcpy(your_question, question);
}
这是GDB错误代码
计划接收信号SIGSEGV , 分段故障。 [切换主题1(LWP 1)] 0x00013af8问题:: set_question(此为0x0 =,=问题0x257a8 “你喜欢电影?”) 在question.cpp:51
51 your_question =新char [strlen(question)+1];
(GDB)p的strlen(问题)
$ 1 = 27
(GDB)p your_question
在地址0x0无法访问存储器