C++:访问冲突写入位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26468459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Access violation writing location
提问by user3001499
Using: MSVS2012
使用:MSVS2012
Code
代码
elemalg.h
elemalg.h
#include <vector>
#include <string>
#include <fstream>
class ElemAlg
{
private:
std::string difficultlyLevel, question, answerToRead;
std::vector<std::string> questions, answers;
std::vector<std::string> GetQuiz(int);
};
elemalg.cpp
elmalg.cpp
#include "elemalg.h"
std::vector<std::string> ElemAlg::GetQuiz(int difficulty)
{
if (difficulty == 1) { difficultyLevel = "algE"; }
if (difficulty == 2) { difficultyLevel = "algM"; }
if (difficulty == 3) { difficultyLevel = "algH"; }
if (difficulty == 4) { difficultyLevel = "algVH"; }
std::ifstream fin(difficultyLevel + ".txt");
while (std::getline(fin, question)) { questions.push_back(question); }
fin.close();
std::ifstream fin2(difficultyLevel + "Answers.txt");
while (std::getline(fin2, answerToRead)) { answers.push_back(answerToRead); }
fin2.close();
return questions;
}
MathTutor.cpp
MathTutor.cpp
#includes etc
ElemAlg *ea;
ea->GetQuiz(1);
GetQuiz
is definitely passed an integer between 1 and 4, this is verified before the method is called
GetQuiz
肯定会传递一个 1 到 4 之间的整数,这是在调用方法之前验证的
difficultyLevel
is a string defined in the header file.
difficultyLevel
是头文件中定义的字符串。
The compiler throws an Unhandled exception and Access violation writing location ... as soon as it hits the first if
function.
编译器会在遇到第一个if
函数时抛出未处理的异常和访问冲突写入位置...。
If I remove the if
functions and define difficultyLevel
as algE just for testing the same problem.
如果我删除if
函数并定义difficultyLevel
为 algE 只是为了测试相同的问题。
If I remove difficultyLevel
entirely and just open the file as "algE.txt"
and "algEAnswers"
then I get the same problem but at a different memory location once the code hits the while loop.
如果我difficultyLevel
完全删除并只打开文件"algE.txt"
,"algEAnswers"
然后我会遇到同样的问题,但是一旦代码遇到 while 循环,就会出现在不同的内存位置。
回答by Sean
Your problem is here:
你的问题在这里:
ElemAlg *ea;
ea->GetQuiz(1);
You're not creating an instance of ElemAlg
, so you're calling a member function on an uninitialized pointer.
您不是在创建 的实例ElemAlg
,因此您是在未初始化的指针上调用成员函数。
Because the member function you are calling isn't virtual the compiler won't 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
) you'll have undefined behaviour. In your case the undefined behaviour leads to an access violation.
因为您调用的成员函数不是虚拟的,所以编译器不必进行任何运行时查找,这就是调用转到GetQuiz
. 但是,this
指针将是垃圾(因为ea
未初始化),所以当您访问成员变量(例如difficultyLevel
)时,您将具有未定义的行为。在您的情况下,未定义的行为会导致访问冲突。
Either initialize ea
:
要么初始化ea
:
ElemAlg *ea=new ElemAlg;
ea->GetQuiz(1)
or, if you don't need to allocate it on the heap just do:
或者,如果您不需要在堆上分配它,请执行以下操作:
ElemAlg ea;
ea.GetQuiz(1)