How to dive dynamic 2D array in totalview

2

Following this thread I would like to share a related issue: how to dive int ** as 2D array in totalview? (in which the solution is discussed here). In case anyone faces the same issue, I hope this would help!

Let's say in my code I have a dynamic 2D array set up as follows

     int ** array2D = new int * [5];
     for (int i = 0; i < 5; i++) {
             array2D[i] = new int [5];
     }

     for (int i = 0; i < 5; i++) {
             for (int j = 0; j < 5; j++) {
                     array2D[i][j] = i + j;
             }
     }

Now, In TotalView, when you dive array2D from Stack Frame, the Variable Window should pop up and display something like this:

Expresion: array2D, Address: 0x7fffd50efbc8
Type: int **
Value: 0x02234660 -> 0x02234690 -> 0x00000000 (0)

The following steps are similar to what Chris Gottbrath mentioned in the thread I mention above, but it gets a bit complicated in terms of when and how exactly we should cast the Type. So,

Step 1: Dive in the value of the pointer to dereference the pointer, by middle-mouse click or double-click the Value.

Doing this should give you something like this:

Expresion: *(((int **)array2D)), Address: 0x02234660
Type: int *
Value: 0x02234690 -> 0x00000000 (0)

Step 2: Cast the type one level now. In "Type" channel, change int * to

int *[5]

Then hit enter. This way the debugger knows that array2D is the pointer that points to a list of pointers with the size of 5. Notice that my dynamic array is a pointer that points to a list of pointers. Now, the Value shown above would change to something like this:

Field                 Value
[0]                   0x02234690 -> 0x00000000 (0)
[1]                   0x022346b0 -> 0x00000001 (1)
[2]                   0x022346d0 -> 0x00000002 (2)
[3]                   0x022346f0 -> 0x00000003 (3)
[4]                   0x02234710 -> 0x00000004 (4)

We now see that array2D has 5 elements, which are pointers to sublists. Now, if we dive in any of these elements, we won't get the list of integers just yet. To see the integers, we need to dereference once more, for each diving we go into.

Step 3: Let's say we want to dive in element [2] to check out the integers from array2D[2][0] to array2D[0][4]. When we middle-click that line in the panel, we see this:

Expresion: (((int *[5])*(((int **)array2D))))[2], Address: 0x02234670
Type: int *
Value: 0x022346d0 -> 0x00000002 (2)

We are now looking at the pointer that points to the sublist number 2, which supposedly contains integer.

Step 4: Now, dive in one more time to dereference, we should see something like this:

Expresion: *(((int *)(((int *[5])*(((int **)array2D))))[2])), Address: 0x022346d0
Type: int
Value: 0x00000002 (2)

Step 5: Cast the type in the second level now. In "Type" channel, change int to

int [5]

Then hit enter. And there we go: we see the elements array2D[2][0] - array2D[2][4]

Field                 Value
[0]                   0x00000002 (2)
[1]                   0x00000003 (3)
[2]                   0x00000004 (4)
[3]                   0x00000005 (5)
[4]                   0x00000006 (6)

Although the diving 1D array is simple, 2D array diving seems to require a careful diving-and-casting order. I have tried other ways, but they didn't work properly. So, I hope this will be useful to someone who stumble upon the same issue.

dynamic
multidimensional-array
totalview
asked on Stack Overflow Jun 12, 2012 by HuaTham • edited May 23, 2017 by Community

1 Answer

1

It's been four years, but I find myself in this situation and found an answer to this question. Most of the answer was found on this knowledge base page.

How to properly display a 2D dynamic array in totalview:

  1. Dive into array2D. Totalview will show the expression as *(array2D) and the type as TYPE *. In your case, TYPE is int.

  2. Change the type entry to TYPE [B]*[A], or equivalently TYPE ([A]*)[B], where you have an array of size A x B. In your case this would be int [5]*[5]. To be clear the valid range of your array should be [0][0] to [A-1][B-1].

  3. Right-click and select Dive into all. This will change the expression to something along the lines of (((TYPE[B]*[A])&*(array2D)))[:] and the type to TYPE[A][B]. The full array will now be shown in Totalview.

answered on Stack Overflow Oct 13, 2016 by rlbond

User contributions licensed under CC BY-SA 3.0