Debugging 如何在gdb中打印二维阵列值

Debugging 如何在gdb中打印二维阵列值,debugging,gdb,Debugging,Gdb,当我使用gdb调试代码时,遇到了一个让我头疼的问题,下面是我的代码片段: int getMaxProfits( int *boards, int length, int consecutive ) { int optBoards[length+3][length+3]; memset(optBoards, 0, sizeof( optBoards ) ); for( int i = length -1; i >= 0; i-- ) { fo

当我使用gdb调试代码时,遇到了一个让我头疼的问题,下面是我的代码片段:

int getMaxProfits( int *boards, int length, int consecutive )
{
    int optBoards[length+3][length+3];
    memset(optBoards, 0, sizeof( optBoards ) );

    for( int i = length -1; i >= 0; i-- )
    {
        for( int j =  i; j <= length - 1; j++ )
        {
            if( j == i )
            {
                optBoards[i][j] = boards[j];
            }
            else if( j - i < consecutive  )
            {
                optBoards[i][j] = boards[j] + optBoards[i][j-1];
            }
.....
我发现事情不像我想的那么简单,它给了我

$1 = 0x7fff5fbff330
看起来像内存地址,然后我试了

p optBoards[0][0]
我得到

我一直在努力

ptype optBoards
我看到

我疯狂地猜测optBoards应该是指向一维数组的指针,因此我再次尝试

p (int[][0])(*optBoards)[0] 
p (int[][0])*((*optBoards)[0])
我又得到了一个内存地址

$2 = 0x7fff5fbff330
我看到了一些希望并再次尝试

p (int[][0])(*optBoards)[0] 
p (int[][0])*((*optBoards)[0])
现在我得到一个大0

$3 = 0x0
我以为我已经得到了我想要的值,后来我发现在我进入for循环后,optBoards将被赋值,但不管怎样,我总是为optBoards的所有元素得到一个大0。我感到迷路了

如何打印此二维数组的正确值

非常感谢你的帮助

你可以按
p((int(*)[8])optBoards)[6][2]
的方式来做,而
8
长度+3
。例如,我有:

#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"

#include <stdio.h>

int foo(int length) {
  int optBoards[length + 3][length + 3];
  int i, j, q;

  for( i = 0, q = 0 ; i < length + 3 ; i++ ) {
    for( j = 0 ; j < length + 3 ; j++ ) {
      optBoards[i][j] = ++q;
      printf("optBoards[%d][%d] = %d\n", i, j, q);
    }
  }

  return 0;
}

int main(int argc, char **argv) {
  foo(5);
  return 0;
}

谢谢。那真的很有帮助。从你的答案来看,似乎我们必须知道数组的长度,然后我们可以将“optBoards”转换为指向固定长度的一维数组的指针。这是我以前不知道的。我不能投你的票,因为我没有足够的声誉。但无论如何,非常感谢。@xinwei很高兴我能帮上忙:)。请考虑接受这个答案,我已经接受了你的答案。抱歉这么晚了,我刚才才知道怎么接受。
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"

#include <stdio.h>

int foo(int length) {
  int optBoards[length + 3][length + 3];
  int i, j, q;

  for( i = 0, q = 0 ; i < length + 3 ; i++ ) {
    for( j = 0 ; j < length + 3 ; j++ ) {
      optBoards[i][j] = ++q;
      printf("optBoards[%d][%d] = %d\n", i, j, q);
    }
  }

  return 0;
}

int main(int argc, char **argv) {
  foo(5);
  return 0;
}
> gcc -Wall file.c -g -o file.exe

> gdb -q file.exe
Reading symbols from file.exe...done.
(gdb) l 18
13            optBoards[i][j] = ++q;
14            printf("optBoards[%d][%d] = %d\n", i, j, q);
15          }
16        }
17
18        return 0;
19      }
20
21      int main(int argc, char **argv) {
22
(gdb) b 18
Breakpoint 1 at 0x4017eb: file file.c, line 18.
(gdb) run
Starting program: file.exe
[New Thread 2912.0xad8]
optBoards[0][0] = 1
...
optBoards[1][2] = 11
...
optBoards[2][7] = 24
...
optBoards[6][2] = 51
...
optBoards[7][7] = 64

Breakpoint 1, foo (length=5) at file.c:18
18        return 0;
(gdb) p length + 3
$1 = 8
(gdb) p ((int (*)[8]) optBoards)[0][0]
$2 = 1
(gdb) p ((int (*)[8]) optBoards)[1][2]
$3 = 11
(gdb) p ((int (*)[8]) optBoards)[2][7]
$4 = 24
(gdb) p ((int (*)[8]) optBoards)[6][2]
$5 = 51
(gdb) p ((int (*)[8]) optBoards)[7][7]
$6 = 64
(gdb)