c++ - 与win 64bit有关吗?错误:STATUS_ACCESS_VIOLATION
问题描述
有谁知道这个错误是否与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)
indexing.
见下文:
#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;
}
这将解决错误的问题,但留给您确定包装逻辑是否准确的任务。我怀疑不是,但这不是这个问题的意义所在。
推荐阅读
- python-3.x - Matplotbib - PercentFormatter - 如何缩放 y 轴并保存绘图?
- c# - 在构建 .NET Core Api 时,将控制器 hepler 方法放置在何处以与设计模式保持一致?
- javascript - 我在节点 js 代码中收到错误意外结束输入
- spring - 使用 Spring-JPA 获得最大价值
- spring-cloud-stream - 在测试 Spring Cloud Stream 功能时,Spring Cloud Contract outputMessage 通道 bean 不是 org.springframework.messaging.PollableChannel
- c# - iOS 加载覆盖不覆盖整个屏幕
- solr - 索引和搜索同时进行时 SOLR 增强查询不起作用
- php - 在写入数据库上运行特定的选择原始查询,以便由于复制滞后而获得新数据
- python-3.x - 从以下列表中提取数据
- javascript - 禁用 Highcharts 中的特定标签