0

I'm writing an object called 'Matrix'. For parameters, the user specifies the number of rows and columns to be created. The constructor is being called in the Main class via:

Matrix* matrix = new Matrix(2, 3);

The constructor is prototyped in an *.h file and is implemented in a *.cpp file as follows (with the private variables included for the sake of clarification):

double** arr;
int rows;
int cols;

Matrix::Matrix(int numRows, int numCols) {
    rows = numRows;
    cols = numCols;
    arr = new double*[numCols];
    for (int i = 0; i < numRows; ++i) {
        for (int x = 0; x < numCols; ++x) {
            arr[i][x] = 0;
        }
    }
}

When the program executes (compiled in Visual Studio) it builds, executes, and throws an exception similar to:

Exception thrown at 0x00091BCC in Matrix Project.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD.

I can only assume this means I'm not looping through the array properly as it breaks on line arr[i][x] = 0 in the nested for loop.

Would anyone be able give me some tips as to why this is breaking? How can I declare the array in a way that doesn't require one long loop being manipulated mathematically?

Thank you in advance!

[EDIT]

Anton provided that I need to allocate the memory to each row. When modifying the original nested loops to the following, the program executed successfully. Thank you!

for (int i = 0; i < numRows; ++i) {
    arr[i] = new double[cols];
    for (int x = 0; x < numCols; ++x) {
        arr[i][x] = 0;
    }
}
Nick
  • 191
  • 1
  • 2
  • 9
  • you have to allocate memory for each row. – antonpp Dec 01 '15 at 00:11
  • Similar to this? for (int i = 0; i < numRows; ++i) { arr[i] = new double[cols]; for (int x = 0; x < numCols; ++x) { arr[i][x] = 0; } } – Nick Dec 01 '15 at 00:14
  • @Anton You are the man. If you create an answer, I'll give you the answer – Nick Dec 01 '15 at 00:15
  • @NickMedovich Try to avoid allocating each row separately if you care at all about efficiency. You can simply do: `double* arr = new double[numRows*numCols];`. To access the matrix at row `r`, column `c`, do: `double data = arr[r*numCols + c];`... or another way: `double* row = arr + r*numCols; double data = r[c];` –  Dec 01 '15 at 00:20
  • Thank you, @Ike. I'll give that a look in a little while. I decided to do it explicitly because my 'partner' in this project is not very familiar with programming paradigms and I figured this would be easier to explain in comments. Otherwise I would have done so with a single array accessed programmatically. I'll give this a go to have a look at the performance enhancements. Thank you! – Nick Dec 01 '15 at 00:23
  • I think you might be able to explain this just as easily potentially with fewer memory blocks to deal with. You can explain it by drawing a grid and showing how the grid cell index is computed from row and column -- similar case for image processing (if you get there) accessing a pixel at a certain row/column... also less memory management stuff with just one array to allocate/free. Anyway -- cheers. –  Dec 01 '15 at 00:25
  • 1
    @Ike, I agree. I'm planning on implementing it as such in the main program. I'm only implementing it this way so my 'partner' understands a little more thoroughly. The actual usage an access of the object will be no more difficult to understand. When we scale up, memory will be a large problem, so I'll have to use a more efficient method, such as yours above. Thank you very much! – Nick Dec 01 '15 at 01:02

0 Answers0