C++ の入門コースを受講していますが、コピー機能でプログラムのセグメンテーション違反が発生しています。GDB を使用すると、位置 0x0 の 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 in question::set_question (this=0x0, question=0x257a8 "映画は好きですか??") at question.cpp:51
51 your_question = new char [strlen(質問)+1];
(gdb) p strlen(質問)
$1 = 27
(gdb) p あなたの質問
アドレス 0x0 のメモリにアクセスできません