Size Specifier: How to display a C/C++ pointer as an array in the Visual Studio debugger

Viewing a C/C++ array in the Visual Studio debugger is easy. You can expand the array in the Watch window (or any other variables window) and see its values. For example, let’s say you have the following C++ code:

int _tmain(int argc, _TCHAR* argv[])
    int numbers[3] = { 1, 2, 3 };

    return 0;

To see the values inside the numbers array, you can simply expand it in the Watch window as shown below.

Display the array numbers in the Watch window

That was easy. Let’s now change the code slightly and add a pointer (line 4 below) that points to the numbers array, as shown in the following code example:

   1:  int _tmain(int argc, _TCHAR* argv[])
   2:  {
   3:      int numbers[3] = { 1, 2, 3 };
   4:      int* p = numbers;
   6:      return 0;
   7:  }

Now, if you expand p in the Watch window, you will only see the first element of p:

Display pointer p in the Watch window 

How can you see all the elements of p? The answer is to use a feature called the “size specifier”. The size specifier is a positive integer that allows you to specify the number of elements that the debugger should display. The size specifier uses the following format:

variable, n

For example, to display all the elements of p, you can add the following expression to the Watch window:

p, 3

The result is shown below.

Display pointer p with size specifier

As I mentioned above, the size specifier should be a positive integer. If you try to specify a negative integer, the debugger displays the following error message:

CXX0026: Error: bad format string

You might ask, what happens if I specify a size that is greater than the size of the array pointer? The answer is that the debugger will happily oblige and display whatever value is in memory. In the screen shot below, I’ve asked the debugger to display the first 10 elements of p, even though it only has 3 elements.

Display p with 10 elements


Unfortunately, the size specifier does not work for C++/CLI. The equivalent C++/CLI code below shows an example of this.

int main(array<System::String ^> ^args)
    array<int>^ arr = gcnew array<int>{1, 2, 3};
    interior_ptr<int> ptr = &arr[0];

    return 0;
Comments (0)

Skip to main content