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

前端 未结 4 668
长情又很酷
长情又很酷 2021-02-19 00:04

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 vari

相关标签:
4条回答
  • 2021-02-19 00:52

    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.

    0 讨论(0)
  • 2021-02-19 00:52

    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

    0 讨论(0)
  • 2021-02-19 00:56

    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.

    0 讨论(0)
  • 2021-02-19 01:07

    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.

    0 讨论(0)
提交回复
热议问题