1

I'm trying to debug some C++ code but I can't see the values in a multi-dimensional array while debugging

I have a dynamically allocated pointer (double **A).

When I try to watch the value of this array I just get the first value, I can't see the remaining values.

Any ideas?

TIA

Tyler
  • 21,762
  • 11
  • 61
  • 90
yomismo
  • 11
  • 2
  • 4

5 Answers5

6

The simplest way to see large data arrays in VS is to use a memory window instead of a Watch window or the Autos or Locals window. Just drag your pointer value to the memory window's address box.

Die in Sente
  • 9,546
  • 3
  • 35
  • 41
2

If you are using Visual Studio, put array[X],Y in the watch window, where X is the line number and Y the number of rows - this will allow you to watch whole lines in the watch window.

For example, put those lines to the watch window:

array[0],7
array[1],7
array[2],7
...
neuromouse
  • 921
  • 1
  • 12
  • 32
0

Iterate through and print out each value. Roughly something like this:

void print2DArray(double **A, int width, int height) {
    for (int i=0; i<width; i++) {
        for (int j=0; j<height; j++) {
            cout<<A[i][j]<<" ";
        }
        cout<<endl;
    }
}
Tyler
  • 21,762
  • 11
  • 61
  • 90
0

Through the debugger you may write explicitly in the watch window A[2][1] etc..

Edited - after the code presented:

int main() {
    double **A; 
    double M = 4;
    A = new double *[M]; //define M by M matrix
    for( int k =0; k < M; k++) { 
        A[k] = new double [M]; 
    } 
    //assign values to matrix 
    for (int i = 0; i < M; i++) { 
        for (int j = 0; j < M; j++) { 
            if ( j == i) { 
                A[i][j] = 2; 
            } else { 
                A[i][j] = 1; 
            }
        }
    }
    return 0;
}

I put break point on the return 0 and add some test values to the watch window:

    A[0][0] 2.0000000000000000  double
    A[0][1] 1.0000000000000000  double
    A[0][2] 1.0000000000000000  double
    A[1][0] 1.0000000000000000  double
    A[1][1] 2.0000000000000000  double
    A[1][2] 1.0000000000000000  double

It seems fine. What do you get when you're doing the same? Where is the problem? You can also print the values to screen as MatrixFrog suggested.

Yaakov Shoham
  • 10,182
  • 7
  • 37
  • 45
  • That doesn't work. I get the values of the first row only so A[0][0] = A[1][0]=A[2][0] and so on. The array is: 2 1 1 1 2 1 1 1 2 So this is clearly wrong. I'm starting to question my decision not use C# for this. – yomismo Dec 30 '09 at 20:34
  • That's very odd. I think you should post the whole code of creating and populating the array. – Yaakov Shoham Dec 30 '09 at 20:56
  • there you go: double **A; double m; A = new double *[M]; //define M by M matrix for( int k =0; k < M; k++) { A[k] = new double [M]; } //assign values to matrix for (int i = 0; i < M; i++) { for (int j = 0; j < M; j++) { if ( j == i) { A[i][j] = 2; } else { A[i][j] = 1; } } – yomismo Dec 31 '09 at 09:12
  • My problem is that I don't get what I should, instead I get A[0][0]=A[1][0] =2 ; A[0][1]=A[1][1] = 1 and so on. In fact, I just stepped through watching all values of A, while assigning the values w and A[1][0] gets assigned at the same time as A[0][0]. This is driving me insane. Help!!!! :) – yomismo Jan 04 '10 at 19:51
  • Try to run **exactly** the code I posted. It's complete program; run this in new empty project. Does it work fine? – Yaakov Shoham Jan 04 '10 at 19:59
0

This is what I get: http://www.flickr.com/photos/42475383@N03/4247049191/

edit.

I was using a CLR console project. I tried a win32 console and it works fine. I can see a google moment coming up to find out what a CLR project is.