访问冲突写入位置动态分配数组
Access violation writing location dynamically allocated array
我正在写一段代码。作为这段代码的一部分,我决定使用一个动态分配的数组来存储一组字符串。但是,当我尝试为我的数组分配一个值时,出现异常
Exception thrown at 0x50D540C9 (vcruntime140d.dll) in Glicko2.exe: 0xC0000005: Access violation writing location 0xC39903E3.
从研究这个主题来看,这个异常似乎主要出现在你尝试写入你的数组之外的时候。但是在我写它的for循环里面,应该不可能在它外面写。异常发生在
行
posResponsesTeamNames[i] = teamName;
我的密码是
int league::matchSet()
{
std::string currLine;
std::string *posResponsesTeamNames = new std::string[league::numTeams];
std::string posResponsesWon[3] = { "1","2","3" };
team* team1;
team* team2;
bool successful = false;
for (int i = 0; i < league::numTeams; i++) {
std::string teamName = league::getName(i + 1);
for (i = 0; i < teamName.size(); i++) {
teamName.at(i) = toupper(teamName.at(i));
}
posResponsesTeamNames[i] = teamName;
}
std::cout << posResponsesTeamNames[1];
roundStart:
for (int i = 0; i < league::numMatches; i++) {
std::cout << "for match " << i + 1 << " please enter name of first team to play: ";
std::getline(std::cin, currLine);
int parsed = parseText(currLine, posResponsesTeamNames);
if (parsed == 2) {
delete[] posResponsesTeamNames;
prepForEnd();
return 2;
}
else if (parsed == 0) {
goto roundStart;
}
for (int i = 0; i < league::teams.size(); i++) {
if (currLine == league::teams.at(i).getName()) {
successful = true;
team1 = &league::teams.at(i);
}
}
if (successful == false) {
std::cout << "ERROR: Invalid team name entered";
goto roundStart;
}
secondTeam:
std::cout << "for match " << i + 1 << " please enter name of second team to play: ";
std::getline(std::cin, currLine);
parsed = parseText(currLine, posResponsesTeamNames);
if (parsed == 2) {
delete[] posResponsesTeamNames;
prepForEnd();
return 2;
}
else if (parsed == 0) {
goto secondTeam;
}
for (int i = 0; i < league::teams.size(); i++) {
if (currLine == league::teams.at(i).getName()) {
successful = true;
team2 = &league::teams.at(i);
}
}
if (successful == false) {
std::cout << "ERROR: Invalid team name entered";
goto secondTeam;
}
whoWon:
std::cout << "please enter 1 if the first team won, 2 if the second team won or 3 if it was a draw: ";
std::getline(std::cin, currLine);
parsed = parseText(currLine, posResponsesWon);
if (parsed == 2) {
delete[] posResponsesTeamNames;
prepForEnd();
return 2;
}
if (parsed == 0) goto whoWon;
}
delete[] posResponsesTeamNames;
}
当您调用“new”关键字时,即调用您的 class 的构造函数。如果您的 constrictor 将您新分配的对象实例化为 null,然后在您的循环中取消对该对象的引用,则可能会造成访问冲突。如果您的限制器以允许立即解除引用(初始化为空字符串“”)的方式初始化您的对象,这可能允许您写入该“新”对象而不会发生访问冲突。
1 std::string currLine;
2 std::string *posResponsesTeamNames = new std::string[league::numTeams];
3 std::string posResponsesWon[3] = { "1","2","3" };
4 team* team1;
5 team* team2;
6 bool successful = false;
7 for (int i = 0; i < league::numTeams; i++) {
8 std::string teamName = league::getName(i + 1);
9 for (i = 0; i < teamName.size(); i++) {
10 teamName.at(i) = toupper(teamName.at(i));
11 }
12 posResponsesTeamNames[i] = teamName;
13 }
假设:league::numTeams 的值为 10
在第 2 行,您正在创建一个大小为 10(league::numTeams) 的数组。
在第 7 行,创建了一个局部变量 counter i。
在第 8 行,创建一个局部变量 teamName
在第 9 行,将 0 分配给 i(在第 7 行创建)并开始循环。你在这个循环中递增它。 teamName 的大小可能超过 10(league::numTeams)。让我们假设 12.
所以i的值可能大于10(league::numTeams)。
现在在第 12 行,您在 posResponsesTeamNames 的位置 i 中赋值,现在是 12。这个任务肯定会失败。
可能的解决方案:在第 9 行,使用 i,而不是使用 int j 作为局部变量 teamName
上的第二个计数器
我希望这能给你答案。
我正在写一段代码。作为这段代码的一部分,我决定使用一个动态分配的数组来存储一组字符串。但是,当我尝试为我的数组分配一个值时,出现异常
Exception thrown at 0x50D540C9 (vcruntime140d.dll) in Glicko2.exe: 0xC0000005: Access violation writing location 0xC39903E3.
从研究这个主题来看,这个异常似乎主要出现在你尝试写入你的数组之外的时候。但是在我写它的for循环里面,应该不可能在它外面写。异常发生在
行 posResponsesTeamNames[i] = teamName;
我的密码是
int league::matchSet()
{
std::string currLine;
std::string *posResponsesTeamNames = new std::string[league::numTeams];
std::string posResponsesWon[3] = { "1","2","3" };
team* team1;
team* team2;
bool successful = false;
for (int i = 0; i < league::numTeams; i++) {
std::string teamName = league::getName(i + 1);
for (i = 0; i < teamName.size(); i++) {
teamName.at(i) = toupper(teamName.at(i));
}
posResponsesTeamNames[i] = teamName;
}
std::cout << posResponsesTeamNames[1];
roundStart:
for (int i = 0; i < league::numMatches; i++) {
std::cout << "for match " << i + 1 << " please enter name of first team to play: ";
std::getline(std::cin, currLine);
int parsed = parseText(currLine, posResponsesTeamNames);
if (parsed == 2) {
delete[] posResponsesTeamNames;
prepForEnd();
return 2;
}
else if (parsed == 0) {
goto roundStart;
}
for (int i = 0; i < league::teams.size(); i++) {
if (currLine == league::teams.at(i).getName()) {
successful = true;
team1 = &league::teams.at(i);
}
}
if (successful == false) {
std::cout << "ERROR: Invalid team name entered";
goto roundStart;
}
secondTeam:
std::cout << "for match " << i + 1 << " please enter name of second team to play: ";
std::getline(std::cin, currLine);
parsed = parseText(currLine, posResponsesTeamNames);
if (parsed == 2) {
delete[] posResponsesTeamNames;
prepForEnd();
return 2;
}
else if (parsed == 0) {
goto secondTeam;
}
for (int i = 0; i < league::teams.size(); i++) {
if (currLine == league::teams.at(i).getName()) {
successful = true;
team2 = &league::teams.at(i);
}
}
if (successful == false) {
std::cout << "ERROR: Invalid team name entered";
goto secondTeam;
}
whoWon:
std::cout << "please enter 1 if the first team won, 2 if the second team won or 3 if it was a draw: ";
std::getline(std::cin, currLine);
parsed = parseText(currLine, posResponsesWon);
if (parsed == 2) {
delete[] posResponsesTeamNames;
prepForEnd();
return 2;
}
if (parsed == 0) goto whoWon;
}
delete[] posResponsesTeamNames;
}
当您调用“new”关键字时,即调用您的 class 的构造函数。如果您的 constrictor 将您新分配的对象实例化为 null,然后在您的循环中取消对该对象的引用,则可能会造成访问冲突。如果您的限制器以允许立即解除引用(初始化为空字符串“”)的方式初始化您的对象,这可能允许您写入该“新”对象而不会发生访问冲突。
1 std::string currLine;
2 std::string *posResponsesTeamNames = new std::string[league::numTeams];
3 std::string posResponsesWon[3] = { "1","2","3" };
4 team* team1;
5 team* team2;
6 bool successful = false;
7 for (int i = 0; i < league::numTeams; i++) {
8 std::string teamName = league::getName(i + 1);
9 for (i = 0; i < teamName.size(); i++) {
10 teamName.at(i) = toupper(teamName.at(i));
11 }
12 posResponsesTeamNames[i] = teamName;
13 }
假设:league::numTeams 的值为 10
在第 2 行,您正在创建一个大小为 10(league::numTeams) 的数组。
在第 7 行,创建了一个局部变量 counter i。
在第 8 行,创建一个局部变量 teamName
在第 9 行,将 0 分配给 i(在第 7 行创建)并开始循环。你在这个循环中递增它。 teamName 的大小可能超过 10(league::numTeams)。让我们假设 12.
所以i的值可能大于10(league::numTeams)。 现在在第 12 行,您在 posResponsesTeamNames 的位置 i 中赋值,现在是 12。这个任务肯定会失败。
可能的解决方案:在第 9 行,使用 i,而不是使用 int j 作为局部变量 teamName
上的第二个计数器我希望这能给你答案。