gpt4 book ai didi

c++ - 是否与 win 64bit 相关?错误 : STATUS_ACCESS_VIOLATION

转载 作者:行者123 更新时间:2023-11-30 03:21:54 24 4
gpt4 key购买 nike

有谁知道这个错误是否与 win64 有关,或者我的代码有问题吗?在窗口 10 - 64 位上,我正在运行 gcc 版本 egcs-2.91.57 19980901(egcs-1.1 版本)——我认为它适用于 32 位

#include <iostream>
#include <fstream>
using namespace std;

const int MAX_STUDENT = 3;
typedef struct student{
char name[50];
char classID[15];
int age;
};
student student_list [MAX_STUDENT];
int student_position = 0;

void add_new(){
if (student_position == MAX_STUDENT) {
student_position = 1;
}
else {
student_position++;
}
cout << "student name: " << endl;
cin >> student_list[student_position].name;
cout << "student classID: " << endl;
cin >> student_list[student_position].classID;
cout << "student age: " << endl;
cin >> student_list[student_position].age ;

}
void list(){
for(int i = 1; i < student_position+1; i ++){
cout << i << "/name: " << student_list[i].name << " - classID: " << student_list[i].classID << " - age: " << student_list[i].age << endl;
}
}

int main()
{
char command = 'a';
cout << "please input command: N (New), L (List), E (Exit), W(Write to file)." << endl;
while(cin >> command){
switch (command) {
case 'N':
add_new();
break;
case 'L':
list();
break;
case 'E':
return 0;
default:
cout << "wrong command" << endl;
break;
}

};
return 0;
}

然后第三次输入学生信息出现错误:

[main] D:\1.books\0.C++_Primer\student.exe 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
[main] student 1000 (0) handle_exceptions: Dumping stack trace to student.exe.core

p/s: 我添加堆栈跟踪:

[main] student 1000 (0) exception: trapped!
[main] student 1000 (0) exception: code 0xC0000005 at 0x407E23
[main] student 1000 (0) exception: ax 0x330000 bx 0x330000 cx 0x418348 dx 0xA
[main] student 1000 (0) exception: si 0x0 di 0x401000 bp 0x246FEA8 sp 0x246FEA4
[main] student 1000 (0) exception: exception is: STATUS_ACCESS_VIOLATION
[main] student 1000 (0) stack: Stack trace:
[main] student 1000 (0) stack: frame 0: sp = 0x246F830, pc = 0x6100A2C3
[main] student 1000 (0) stack: frame 1: sp = 0x246F86C, pc = 0x7783EAC2
[main] student 1000 (0) stack: frame 2: sp = 0x246F890, pc = 0x7783EA94
[main] student 1000 (0) stack: frame 3: sp = 0x246F95C, pc = 0x7782C6B6
[main] student 1000 (0) stack: frame 4: sp = 0x246FEA8, pc = 0x407AB1
[main] student 1000 (1) stack: frame 5: sp = 0x246FED0, pc = 0x4011A2
[main] student 1000 (0) stack: frame 6: sp = 0x246FEE4, pc = 0x401637
[main] student 1000 (0) stack: frame 7: sp = 0x246FF00, pc = 0x61004402
[main] student 1000 (0) stack: frame 8: sp = 0x246FF48, pc = 0x61004420
[main] student 1000 (0) stack: frame 9: sp = 0x246FF54, pc = 0x41772E
[main] student 1000 (0) stack: frame 10: sp = 0x246FF64, pc = 0x40103A
[main] student 1000 (0) stack: frame 11: sp = 0x246FF80, pc = 0x77318484
[main] student 1000 (0) stack: frame 12: sp = 0x246FF94, pc = 0x77822FEA
[main] student 1000 (0) stack: frame 13: sp = 0x246FFDC, pc = 0x77822FBA
[main] student 1000 (0) stack: frame 14: sp = 0x246FFEC, pc = 0x0
[main] student 1000 (0) stack: End of stack trace

最佳答案

您的索引是错误的。

C 和 C++ 都使用基于零的索引。这意味着维度 N 的原生数组可以使用索引 0..(N-1) 访问。您 student_position 应该将明显的翻转标记回零,您似乎在遇到插入请求时已经达到最大数组维度时正在寻求。显示循环应该从零到维度,在顶端严格小于(因此符合 0..(N-1) 索引。

见下文:

#include <iostream>
#include <fstream>
using namespace std;

const int MAX_STUDENT = 3;
struct student
{
char name[50];
char classID[15];
int age;
};
student student_list [MAX_STUDENT];
int student_position = 0;

void add_new(){
if (student_position == MAX_STUDENT) {
student_position = 0; // HERE
}

cout << "student name: " << endl;
cin >> student_list[student_position].name;
cout << "student classID: " << endl;
cin >> student_list[student_position].classID;
cout << "student age: " << endl;
cin >> student_list[student_position].age ;

++student_position; // HERE

}
void list(){
for(int i = 0; i < student_position; ++i){
cout << i << "/name: " << student_list[i].name << " - classID: " << student_list[i].classID << " - age: " << student_list[i].age << endl;
}
}

int main()
{
char command = 'a';
cout << "please input command: N (New), L (List), E (Exit), W(Write to file)." << endl;

while(cin >> command){
switch (command) {
case 'N':
add_new();
break;
case 'L':
list();
break;
case 'E':
return 0;
default:
cout << "wrong command" << endl;
break;
}
}

return 0;
}

这将解决故障问题,但您需要确定您的换行逻辑是否准确。我怀疑不是,但这不是这个问题的问题。

关于c++ - 是否与 win 64bit 相关?错误 : STATUS_ACCESS_VIOLATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51721093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com