view multi-dimensional array values debugging in Visual C++
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 t开发者_运维问答he value of this array I just get the first value, I can't see the remaining values.
Any ideas?
TIA
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.
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
...
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;
}
}
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.
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.
精彩评论