Memory Violation in c++

177 views Asked by At

I'm getting the following error when I run my code:

Unhandled exception at 0x00F66754 in KSU.CIS308.Project5.exe: 0xC0000005: Access violation writing location 0xFDFDFDFD.

I assume it is due to having memory for the pointer but not what it is pointing too. I have no clue how to assign it though. Here is the code, it is the constructor method for the Matrix objects:

Matrix::Matrix(int row, int col)
{
    this->rows = row;
    this->cols = col;
    this->arr = new int*[row];
    for (int i = 0; i < row; i++)
    {
        this->arr[i] = new int[col];
    }
}

The this->arr = new int*[row]; is what is throwing it. It is a pointer to a pointer so that I can store the matrix like a 2-d array. Any help is much appreciated. I think it is also being thrown later but if I can figure one out the rest should be easy to fix as well.

Arr is declared like this in the header file:

int **arr;

Okay so when debugging I found that col is getting passed 0 which might explain the error. I thought I calculated the right value for passing it. Here is the main file. I use to strtok's to put everything into a Vector. User input format is: "1 2 3 // 3 2 1" Where "//" is row break.

int main() {
    vector<int> v1;
    int rowCountA = 1;
    int colCountA = 0;
cout << "Enter First Matrix (put // to denote row breaks): ";
    char buff[200];
    char *token;
    char *tok;
    scanf("%s", buff);
    token = strtok(buff, "//");
    while (token != NULL)
    {
        tok = strtok(token, " ");
        while (tok != NULL)
        {
            v1.push_back(atoi(tok));
            tok = strtok(NULL, " ");
            colCountA++;
        }

        token = strtok(NULL, "//");
        rowCountA++;
    }



    A = new Matrix(rowCountA, colCountA/(rowCountA+1));

    for (int i = rowCountA; i > 0; i--)
    {
        for (int k = colCountA; k < 1; k--)
        {
            A->setElem(i, k, v1.back());
            v1.pop_back();
        }
    }
}
1

There are 1 answers

0
outlyer On

As you've found out, the problem is passing 0 as one of the dimensions, the 2D-array creation is otherwise correct. You shouldn't ever allow col to be 0, worst case scenario (let's say empty input), make it be 1.
Anyway you aren't parsing the input correctly:

scanf("%s") is returning only the first number, because it stops at the first space. Your code should use fgets() instead.

Using C string functions, you should replace your scanf("%s", buff); with:

fgets(buff, sizeof(buff)/sizeof(char), stdin);

But, you can't nest calls to strtok(). When you split the string on // and then call strtok on each token, you can no longer retrieve the next row because it forgets about the tokenizing based on //.

Here is a rough equivalent to the parsing you're doing using C++ i/o (although the code does have some other errors):

string sbuff;
getline(cin, sbuff);
stringstream ss(sbuff); // header file <sstream>
while (ss >> sbuff) {
    if (sbuff == "//") {
        rowCountA++;
    }
    v1.push_back(atoi(sbuff.c_str()));
    colCountA++;
}