简体   繁体   中英

gdb - how to view contents of an array of pointer?

I am having a problem in getting the contents of the variable 'arr' which is an array of pointers. I tried, p *arr@n , but it gives the following output: $1 = {0x603010, 0x603030} . What should I do?

int n, q;
scanf("%d %d", &n, &q);

int lastAnswer=0, index_size[n], *arr[n];  // <-- here
for(int i=0; i<n; i++)
    index_size[i] = 0;

for(int i=0; i<n; i++) {

    int *temp = malloc(sizeof(int)*n);
    arr[i] = temp;
}

while(q--) {
    int w, x, y, seq;
    scanf("%d %d %d", &w, &x, &y);

    if(w == 1) {

        seq = ((x ^ lastAnswer) % n);
        arr[seq][index_size[seq]++] = y;
    }
    else {

        seq = ((x ^ lastAnswer) % n);
        lastAnswer = y%n;
        printf("%d\n", lastAnswer);
    }
}

return 0;

If you print out a pointer itself, it would just give you an address in your memory block.

So print *arr@n would simply give you the content of the first dimension (an array of address in your output)

If you want to print out the deeper content. You might want to do something like this:

print **arr@n;

or

print *arr[0]@n

Another method would be define a pretty print function inside your program and call it in gdb.

void print(int arr[][], n, m)
{
    int i, j;
    for (i = 0; i < n; ++i)
    {
        for (j = 0; j < m; ++j)
            printf("%d ", arr[i][j]);
        printf("\n");
    }
}

And call it in gdb by

call print(arr, n, m)

I don't think gdb support printing 2D array itself, why? Because the definition of print *array@3 isn't printing the first three elements in array , instead, it is "priting *array (or array[0] ) and the three elements following array[0] .

print **arr@n@n

would not work in this case, (although it print out an nice format)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM