Reputation: 11
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
Upvotes: 1
Views: 6772
Reputation: 941
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
...
Upvotes: 3
Reputation:
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.
Upvotes: 0
Reputation: 10548
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.
Upvotes: 0
Reputation: 9937
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.
Upvotes: 6
Reputation: 22116
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;
}
}
Upvotes: 0