gdb-如何查看指针数组的内容? [英] gdb - how to view contents of an array of pointer?
问题描述
在获取变量'arr'的内容时遇到了问题,该变量是一个指针数组.
我尝试了p *arr@n
,但是它给出了以下输出:$1 = {0x603010, 0x603030}
.
我该怎么办?
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.
所以print *arr@n
只会为您提供第一维的内容(输出中的地址数组)
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;
或
print *arr[0]@n
另一种方法是在程序内部定义一个漂亮的打印函数,然后在gdb中调用它.
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");
}
}
然后在gdb中调用它
call print(arr, n, m)
我不认为gdb本身支持打印2D数组,为什么?
因为print *array@3
的定义不是打印array
中的前三个元素,所以它是定价*array
(或array[0]
)以及array[0]
之后的三个元素.
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)
这篇关于gdb-如何查看指针数组的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!