How to Display a Dynamically Allocated Array in the Visual Studio Debugger

How to display a dynamically allocated array in the Visual Studio debugger?

Yes, simple.
say you have

char *a = new char[10];

writing in the debugger:

a,10

would show you the content as if it were an array.

How to see what are the contents of dynamically allocated array while debugging in VS 2010?

It is quite simple, F.e. you have:

char* ptr = new char[10];

Then if you write in debugger:

ptr,10

it will show you content as if it were static array.

In Visual Studio 2019 C++, how can I expand a dynamically allocated array so that all of its elements are displayed?

It seems that I found how to do this using .natvis files.

This article provides more details about Natvis files:
https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=vs-2019

Adding a .natvis file to your project allows you to specify how the container should be displayed in Locals.

Here is a simple example for the Vector container described in the original post:

<?xml version="1.0" encoding="utf-8"?> 
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="AC::Vector<*>">
<DisplayString>{{ size={size} }}</DisplayString>
<Expand>
<Item Name="[size]" ExcludeView="simple">size</Item>
<ArrayItems>
<Size>size</Size>
<ValuePointer>data</ValuePointer>
</ArrayItems>
</Expand>
</Type>

</AutoVisualizer>

After creating the file and starting the debug session, the container now displays its contents properly:

AC::Vector<int> myVec(3);
myVec[0] = 1;
myVec[1] = 2;
myVec[2] = 3;

Locals:

Sample Image

View array in Visual Studio debugger?

You can try this nice little trick for C++. Take the expression which gives you the array and then append a comma and the number of elements you want to see. Expanding that value will show you elements 0-(N-1) where N is the number you add after the comma.

For example if pArray is the array, type pArray,10 in the watch window.

(MSVC-2019) Is there a way to see the array's elements made with malloc in the debug(autos) screen?

That is normal and autos window cannot specify malloc type and it should work with the obvious definition size of your arr.

Instead, just as the community members said, use Watch Window, set arr,10 to get that you want. And then it will parse the array with the input size.

Sample Image

How to add debug watch for dynamically allocated array in CodeLite?

I've had some success using a casting style syntax for the watch value:

(int[10]*)a

This shows all array values once expanded in the watch window. The declaration of a in the code was:

int *a = new int[10];

Here is the watch window:

Sample Image



Related Topics



Leave a reply



Submit