Emre Turkoz
Emre Turkoz

Reputation: 868

How to see all elements of a two dimensional array in Visual Studio 2010?

I'm debugging my c++ code in Visual Studio 2010 and want to see the content of my array, say Q, which is 17x17. When I insert a breakpoint and try to debug, I see only the variable "Q". When I take it to the "Watch" screen and rename it to "Q,17", I see one level down.

But I want to see the other dimension,too. I can't write "Q,17,17". What is the proper command?

Thank you...

Upvotes: 19

Views: 18733

Answers (5)

hrdom
hrdom

Reputation: 168

Statically allocated global memory or local variables can be viewed directly

I didn't find a way to view dynamically allocated 2-D arrays in VS2010 (only 1-D). VS2019 can, through

double ** a;
a = (double**)malloc(5 * sizeof(double*));
a[0]=(double*)malloc(10 * sizeof(double));
...

(double(*)[10]) a[0],5 (add in watch window)

image

And this one down here will also work (double(**)[10])a,5

Upvotes: 0

David
David

Reputation: 231

If you want to see the values organized in 2D in a more graphical way, you can try the Array Visualizer extension. It works fine with small multidimensional arrays.

It is a free, open source extension which can be downloaded via MarketPlace. It is designed to display arrays while debugging an application. There are versions for visual studio 2010, 2012, 2013 and 2015, unfortunatelly it seems not to have been updated to 2017.

Upvotes: 2

Tin Luu
Tin Luu

Reputation: 1687

Put the array Q to global scope and you can see all it's elements(if it is local array you can copy to a global array and manipulate on global array):

int Q[17][17];
int main(){
    int x=1, y=1, z;
}

After debuging and the algorithm are well verified, you can use the local array as you want

Upvotes: 0

Pierre
Pierre

Reputation: 31

The solution proposed only works with 1D arrays. But a 2D array that has fixed size in each row (seeing the first dimension as a row as in maths) can be allocated as a 1D array as follows:

int ** a = new int * [n];
int * b = new int [n*n];

a[0] = &b[0];
for (int i=1; i<n; i++)
{
    a[i] = a[i-1]+n;
}
int count=0;
for (int i=0; i<n; i++)
{
    for (int j=0; j<n; j++)
    {
        a[i][j]= rgen.randInt(-10,10);
    }
}

You can then use a as a matrix in your code and visualise using say b,100 if your matrix is 10 by 10.

Upvotes: 0

John Dibling
John Dibling

Reputation: 101456

You can't, at least not directly.

What you can do is put &array[0][0] in the memory window, and then resize it so the number of columns matches one row of array data.

Alternatively, you can put array[0],17 in the watch window, and then repeat it for array[1],17, etc.

Not the answer you were looking for perhaps, but the watch window, while pretty powerful, just can't do what you want.

Upvotes: 22

Related Questions