作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我使用 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];
}
.....
p optBoards
$1 = 0x7fff5fbff330
p optBoards[0][0]
Cannot perform pointer math on incomplete types, try casting to a known type, or void *.
ptype optBoards
type = int [][0]
p (int[][0])(*optBoards)[0]
$2 = 0x7fff5fbff330
p (int[][0])*((*optBoards)[0])
$3 = 0x0
最佳答案
你可以这样做 p ((int (*)[8]) optBoards)[6][2]
, 而 8
是 length + 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;
}
> 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)
关于debugging - 如何在gdb中打印二维数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497339/
我是一名优秀的程序员,十分优秀!