C++: Access violation writing location

C++: Access violation writing location

Your problem is here:

ElemAlg *ea;
ea->GetQuiz(1);

Youre not creating an instance of ElemAlg, so youre calling a member function on an uninitialized pointer.

Because the member function you are calling isnt virtual the compiler wont have to do any runtime lookup, which is why the call goes to GetQuiz. However, the this pointer will be garbage (as ea is uninitialized), so the moment you access a member variable (such as difficultyLevel) youll have undefined behaviour. In your case the undefined behaviour leads to an access violation.

Either initialize ea:

ElemAlg *ea=new ElemAlg;
ea->GetQuiz(1)

or, if you dont need to allocate it on the heap just do:

ElemAlg ea;
ea.GetQuiz(1)

C++: Access violation writing location

Leave a Reply

Your email address will not be published.