c++ - 为什么 gdb 无法打印数组元素?

标签 c++ arrays gcc printing gdb

我希望使用 gdb 打印已分配内存的所有数组元素。

int main() {
   int* p=(int*)malloc(7*sizeof(int)); 
   for(size_t j=0;j<7;++j) {
         p[j]=j; 
   } 
   return 0; 
} 

所以我用-g3编译这个程序,用gdb启动它,设置break

> print p@7 

我预计 with 会打印出 1-7,但实际输出是

$1 = {0x6160b0, 0x5, 0xff00000000, 0x615c60, 0x615c80, 0x615c20, 0x615c40} 

为什么我的期望!=实际结果,我的程序有什么问题吗?

最佳答案

GDB Manual说:

The left operand of `@' should be the first element of the desired array and be an individual object. The right operand should be the desired length of the array. The result is an array value whose elements are all of the type of the left argument

如果你说

 print p@7 

您告诉 GDB 打印一个包含 7 个指针的数组。

你需要的是:

print *p@7 

关于c++ - 为什么 gdb 无法打印数组元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39286010/

相关文章:

c++ - 汇编程序和 C++ 输出文本

move 操作后 C++ lambda ‘this’ 指针失效

python - 将 2-D numpy 数组附加到 3-D numpy 数组

c - C中的按位 "not"运算符返回带符号的结果

c++ - 移位编译器错误或极端情况?

c - 如何在带有 GCC 的 C 程序中的函数之间暂停?

c++ - 如何从 XLL UDF 返回数组

c++ - leetcode 65. 有效数字如何理解状态转移表

php - 使用数组的数组填充表单选项列表

java - 尝试在 Java 中检查重复项