My program is running into "Process returned -1073741819 (0xC0000005)" while attempting to append a data set into a class object

-4

I am currently trying to append a set of data that is seperated by a comma like this:

lastname, firstname, id-number

lastname2, firstname2, id-number2

etc...

into a class object that looks like this:

class Customers{
public:
    string lastname;
    string firstname;
    string id;
public:
    void setLN (string ln) {lastname = ln;}
    void setFN (string fn) {firstname = fn;}
    void setID (string ident) {id = ident;}
};

template <class T>
class hashTable{
private:
    node<T> *harray[10], *tarray[10];
public:
    void hashBrown(){
        fstream inputFile;
        int totalNum = 0;
        int ln = 0;
        int fn = 0;
        int idn = 0;
        string line;
        inputFile.open("Customer.csv", ios::in|ios::binary);
        if(inputFile.is_open()){
            while(getline(inputFile, line)){
                totalNum++;
                Customers obj[totalNum];
                istringstream iss(line);
                string token;
                getline(iss, token, ',');
                cout<<token<<" ";
                obj[totalNum].setLN(token);

                getline(iss, token, ',');
                cout<<token<<" ";
                obj[totalNum].setFN(token);

                getline(iss, token);
                cout<<token<<"\n";
                obj[totalNum].setID(token);

            }
            cout << totalNum;
        }
    }
};

however the program then crashes on the third token of the first line, and gives me the following error.

Process returned -1073741819 (0xC0000005) execution time : 1.544 s

This all works fine when I comment out

obj[totalNum].setLN(token);

obj[totalNum].setFN(token);

obj[totalNum].setID(token);

these three lines, but I don't suspect that is the issue because when I add

cout<<obj[totalNum].lastname;

cout<<obj[totalNum].firstname;

cout<<obj[totalNum].id;

it prints out the proper values but crashes immediately following that. I've been on this for an hour and I know I'm missing something, but I cant exactly figure out where and what.

c++
asked on Stack Overflow Feb 1, 2019 by Will K

2 Answers

0

There are several issues that look suspicious in the program. You are creating, for example, a local variable of type "array of Customer" within a loop body; it will be initialized again and again with every iteration of the loop, and it will get invalid / go out of scope once the loop has finished.

Anyway, the following statement will yield undefined behaviour:

 Customers obj[totalNum];
 ...
 obj[totalNum].setLN(token);

This is because - regardless what the value of totalNum is - you will access an element one behind the size of the array. Note that when you crete an array of size 10, then the range of valid indizes is [0..9] not [1..10].

To overcome this, you'd write either

 Customers obj[totalNum+1];
 ...
 obj[totalNum].setLN(token);

or

 Customers obj[totalNum];
 ...
 obj[totalNum-1].setLN(token);  // provided that totalNum is always >= 1

But - as mentioned in the beginning - there seam to be other things to think over as well.

BTW: Customers obj[totalNum]; defines a variable length array, which is not supported by standard C++ (though some compilers support it).

answered on Stack Overflow Feb 1, 2019 by Stephan Lechner • edited Feb 1, 2019 by Stephan Lechner
0

thanks guys!

template <class T>
class hashTable{
private:
    node<T> *harray[10], *tarray[10];
public:
    void hashBrown(){
        fstream inputFile;
        int totalNum = 0;
        int currentNum = 0;
        int fn = 0;
        int idn = 0;
        string line;
        inputFile.open("Customer.csv", ios::in|ios::binary);
        if(inputFile.is_open()){
            while(getline(inputFile, line)){
                totalNum++;
            }
        }
        inputFile.close();
        Customers obj[totalNum];
        inputFile.open("Customer.csv", ios::in|ios::binary);
        if(inputFile.is_open()){
            while(getline(inputFile, line)){
                istringstream iss(line);
                string token;
                getline(iss, token, ',');
                cout<<token;
                obj[currentNum].setLN(token);
                cout<<obj[currentNum].lastname;

                getline(iss, token, ',');
                obj[currentNum].setFN(token);
                cout<<obj[currentNum].firstname;

                getline(iss, token, ',');
                obj[currentNum].setID(token);
                cout<<obj[currentNum].id<<"\n";

                currentNum++;

            }
        }
    }
};

probably really inefficient, but it solves the problem

answered on Stack Overflow Feb 1, 2019 by Will K

User contributions licensed under CC BY-SA 3.0