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

2
  • Is this really a multi-dimensional array or is it an array of arrays? Commented Dec 30, 2009 at 20:25
  • it's an array of arrays. Was not aware of a difference Commented Dec 31, 2009 at 9:16

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This actually solves the problem, don't know why it is not selected as an answer...
3

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
...

Comments

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;
    }
}

Comments

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.

5 Comments

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.
That's very odd. I think you should post the whole code of creating and populating the array.
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; } }
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!!!! :)
Try to run exactly the code I posted. It's complete program; run this in new empty project. Does it work fine?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.