代码之家  ›  专栏  ›  技术社区  ›  zoro

是否与win 64位相关?错误:状态访问冲突

c++
  •  1
  • zoro  · 技术社区  · 7 年前

    是否有人知道此错误与win64有关,或者我的代码有问题? 在windows10-64bit上,我运行的是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
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   WhozCraig    7 年前

    您的索引错误。

    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;
    }
    

    这将解决故障问题,但留给您的任务是确定wrap逻辑是否正确。我想不是,但这不是问题所在。