I am in a introductory C++ course and my program is segfaulting in my copy function. when I use GDB it says it cant access the char * at location 0x0. the odd thing is that I have written this function many times before without any errors.
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);
}
this is the error code in GDB
Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 1 (LWP 1)] 0x00013af8 in question::set_question (this=0x0, question=0x257a8 "do you like movies??") at question.cpp:51
51 your_question = new char [strlen(question)+1];
(gdb) p strlen(question)
$1 = 27
(gdb) p your_question
Cannot access memory at address 0x0