gpt4 book ai didi

c++ - 在 GDB 中打印 int **x

转载 作者:太空狗 更新时间:2023-10-29 21:35:28 25 4
gpt4 key购买 nike

我有一个指向数组的指针数组(称为板)。我想以正常方式打印它(这在 GDB 中很难)。

int col = 64, row = 2;
int **board = new int*[col];
for(int i = 0; i < col; i++)
board[i] = new int[row];

我尝试了以下命令:

p *array@len (the problem is it prints in hexadecimal values not as integer)

x/100w array (It scrolls so much down that i cannot even see the values)

*(T (*)[N])p (where p is array, T is type of array and N is size of it) [It just does not print accurate]

最佳答案

I want to print it in normal way

最好的办法是编写一个 print_board 例程,然后您可以从 GDB 调用它。

(which is pretty hard in GDB).

那是因为让它变得困难了。

问题是你的板子,而不是在内存中是连续的(按照惯例,在 GDB 中打印是微不足道的),而是分散在 64 个独立的 block 中,每个 block 有 2 个值。

由于您使用的是 C++,因此最好使用 vector 的 vector :

vector<vector<int>> board;
board.resize(col);
for (int i = 0; i < col; i++) {
board[i].resize(row);
}

(gdb) print board
$1 = std::vector of length 64, capacity 64 =
{std::vector of length 2, capacity 2 = {0, 0},
std::vector of length 2, capacity 2 = {0, 0},
...

附言提问时,显示实际 代码会有所帮助。您的问题的 board 类型错误,并且将 boardarray 混合在一起。

更新:

How can I do that (use print_board from GDB)?

这是一个例子。假设您的源代码如下所示:

 1  #include <stdio.h>
2
3 int main()
4 {
5 int col = 16, row = 2;
6 int **board = new int*[col];
7
8 for (int i = 0; i < col; i++) {
9 board[i] = new int[row];
10 }
11
12 // Initialize elements to something interesting, so we can see them
13 // printed.
14 for (int i = 0; i < col; i++)
15 for (int j = 0; j < row; j++)
16 board[i][j] = 100*i + j;
17
18 return 0;
19 }
20
21 void print_board(int **board, int col, int row)
22 {
23 for (int j = 0; j < row; j++) {
24 for (int i = 0; i < col; i++) {
25 printf("\t%d", board[i][j]);
26 }
27 printf("\n");
28 }
29 }

然后,使用 GDB:

gdb -q ./a.out
(gdb) break 18
(gdb) run

Breakpoint 2, main () at t.cc:18
18 return 0;

(gdb) call print_board(board, col, row)
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500
1 101 201 301 401 501 601 701 801 901 1001 1101 1201 1301 1401 1501

关于c++ - 在 GDB 中打印 int **x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42353006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com