Alex Rivera | Logout

View Array contents in Qt Creator debugger

Asked 2011-09-23T19:25:51.077
15

I am using Qt on Ubuntu. When I debug I only see the very first value of the array in Locals and Watchers. How can I view all the array contents?

struct node
{
    int *keys;
    void **pointers;
    int num_keys;
    struct node *parent;
    int is_leaf;
    struct node *nextLevelNode;
};

It shows only the first key value in the debugging window.

Edit
Report

1 Answer

8

It shows only the first key value,in the debugging window

I presume you're referring to the pointer keys, declared with int *keys;

The debugger doesn't know that this is an array: all it knows is that this is a pointer to an int. So it can't know how many values you want it to display.

What I've found, using the Qt Creator 2.1.0 debugger on Ubuntu, is that the following code allows me to see all 5 values:

int array1[5];
array1[0] = 2;
array1[1] = 4;
array1[2] = 6;
array1[3] = 8;
array1[4] = 10;

Whereas with this code, the debugger only shows the first value, exactly as you describe.

int* array2 = new int[5];
array2[0] = 20;
array2[1] = 21;
array2[2] = 22;
array2[3] = 23;
array2[4] = 24;

Aside: of course, the above code would be followed by this, to avoid leaking memory:

delete[] array2;

Later: This Qt Developer Network Forum Post says that you can tell the debugger to display a pointer as an array:

In Locals and Watchers, context menu of your pointer’s entry, select “Watch Expression”. This creates a new watched expression below.

There, double click on the entry in the “Names” column, and add “@10” to display 10 entries.

This sounds like it should get you going.

answered 2011-09-24T17:57:35.400

Your Answer