标签存档: debug

Visual C++调试时查看数组内容

微软文档原文地址:

http://support.microsoft.com/?scid=kb%3Ben-us%3B198953&x=9&y=9

Starting with Visual C++ version 6.0, it is now possible to expand an array pointer to view all array elements in the Visual C++ Debugger Watch window. This feature is not documented.
从 Visual c + + 6.0 版开始,现在可以展开 Visual c + + 调试器监视窗口中查看所有数组元素的数组指针。 此功能未记录。

In the Watch window, type an expression that evaluates to a pointer followed by a comma and the number of elements in the array.
在监视窗口中键入一个表达式,该表达式计算为后跟一个逗号和数组中的元素数的指针。

  1. 作为控制台应用程序生成下面的代码的调试版本。
       // Filename main.cpp
       // No compile option needed
    
       #include <iostream.h>
    
       void main(void)
       {
         int * p;
         char* ptr = "Hello World";
         p = new int [10];
    
         for(int i=0; i<=9; i++){*(p+i) = i+1;}
         cout << i <<endl;
       }
  2. 使用调试器单步执行该代码,并在最后一行代码处停止。
  3. 在监视或 Quickwatch 窗口中添加变量 pptr。 您将看到变量旁边的 + 符号。
  4. 单击 + 符号以展开该变量。 您将看到它指向的数组中,只有第一个元素。
  5. 在监视窗口中现在,键入 p,10ptr,11
  6. 单击 + 符号以展开该变量。 现在,您看到它指向的数组的所有元素。

如果您要查看特定范围的元素,然后输入第一个元素来指定在上述步骤中所述跟格式说明符将起始索引的地址。 例如,(p+3),8 显示元素 p [3..10] 和 (ptr+3),10 显示元素 ptr [3..12]。 在监视窗口中的起始索引是 [0] 的不幸的是,这实际上对应于在此示例中的索引 3。 您必须记住偏移量 3 具有要添加到每个显示的索引,以获取数组元素的实际索引。

Canonical URL by SEO No Duplicate WordPress Plugin