-1

doing some assignment, here's a function to count negative numbers in a dynamically allocated 2D array:

 void numberOfNegs(int** arrayPointer, int n) {
    int counter{ 0 };
    for (int i{ 0 }; i < n; i++){
        for (int j{ 0 }; j < n; j++){
            if (arrayPointer[i][j] < 0) {
                counter++;
            }
        }
    }

Seems legit to me, but the debugger throws this error:

Unhandled exception at 0x00C25D9A in *.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD.

Please, help

Here's more code on how I casted it

    std::cin >> x;

int** theMatrix = new int*[x];
for (int i{ 0 }; i < x; i++){
    theMatrix[x] = new int[x];
}

std::cout << "Please enter a matrix " << x << std::endl;

for (int i{ 0 }; i < x; i++) {
    for (int j{ 0 }; j < x; j++) {
        std::cin >> theMatrix[x][x];
    }
}

 numberOfNegs(theMatrix, x)
small-j
  • 309
  • 1
  • 4
  • 12
  • What are you passing in? – marsh Mar 18 '15 at 18:14
  • If your data structure isn't sane, there's no way this will work. Wouldn't using `std::vector` save you a lot of pain here? That `for (int j{0}; ...` notation is downright bizarre, too. Why not `int j = 0`? – tadman Mar 18 '15 at 18:14
  • You should try debugging. And also post a minimal code sample that reproduces the problem. – Andreas Vennström Mar 18 '15 at 18:15
  • 1
    How did you allocate `arrayPointer`? More code, please... – defube Mar 18 '15 at 18:15
  • 1
    There's nothing wrong with the code you posted, we need more information. – Emil Laine Mar 18 '15 at 18:16
  • 2
    `0xcdcdcdcd` is a special bit pattern in the MSVC debug heap implementation with which newly allocated memory is overwritten. It indicates that you're trying to use uninitialized memory as a pointer and dereference it, so chances are that the inner arrays of your 2D array are not all properly allocated/put into the outer array. – Wintermute Mar 18 '15 at 18:17
  • 1
    Here is more info on 0xcdcdcdcd: http://stackoverflow.com/a/127404/487892 – drescherjm Mar 18 '15 at 18:17
  • Added the allocation code – small-j Mar 18 '15 at 18:33
  • i wonder why you you this fancy curly brackets everywhere and at the same time a "dynamically allocated 2d array" instead of a standard container. Looks like an anachronism to me :P – 463035818_is_not_an_ai Mar 18 '15 at 18:34
  • it's just a college assignment,need to follow the task – small-j Mar 18 '15 at 18:57

1 Answers1

2

Your initialisation problem is here:

for (int i{ 0 }; i < x; i++){
    theMatrix[x] = new int[x];
}

You are using x as the array index, while your (probably) mean i. Your current code just creates an array for the last element, x times. Change it to:

for (int i{ 0 }; i < x; i++){
    theMatrix[i] = new int[x];
}

You may also want to adjust this code:

for (int i{ 0 }; i < x; i++) {
    for (int j{ 0 }; j < x; j++) {
        std::cin >> theMatrix[x][x];
    }
}

To:

for (int i{ 0 }; i < x; i++) {
    for (int j{ 0 }; j < x; j++) {
        std::cin >> theMatrix[i][j];
    }
}
matsjoyce
  • 5,744
  • 6
  • 31
  • 38