related to win 64bit or not? error: STATUS_ACCESS_VIOLATION

1

does anyone know if this error is related to win64 or is there something wrong with my code please ? On window 10 - 64bit, I'm running gcc version egcs-2.91.57 19980901 (egcs-1.1 release) -- which I think is for 32bit

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

then the error occurred for the third time input student information:

[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: I add the stack trace:

[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++
asked on Stack Overflow Aug 7, 2018 by zoro • edited Aug 7, 2018 by zoro

1 Answer

2

Your indexing is wrong.

Both C and C++ utilize zero-based indexing. That means native arrays of dimension N are accessible using indexes 0..(N-1). You student_position should flag the apparent rollover back to zero you seem to be seeking upon encountering an insert request when it has already attained the maximum array dimension. And the loops for display should run from zero to the dimension, strictly less-than on the top end (therefore complying with 0..(N-1) indexing.

See below:

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

That will solve the problem of the fault, but leave you with the task of determining whether your wrap-logic is accurate. I suspect it isn't, but that isn't what this question is about.

answered on Stack Overflow Aug 7, 2018 by WhozCraig • edited Aug 7, 2018 by WhozCraig

User contributions licensed under CC BY-SA 3.0