View Array contents in Qt Creator debugger

2020-03-09 08:57发布

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.

5条回答
手持菜刀,她持情操
2楼-- · 2020-03-09 09:14

In Expression evaluator, Try (int[10])(*myArray) instead of (int[10])myArray

Or, *myArray@10 instead of myArray@10

查看更多
▲ chillily
3楼-- · 2020-03-09 09:21

In Qt for mac what worked for me was:

  1. Add an expression evaluator for the desired variable (right click the variable on the debugger window then "Add expression evaluator for "var name here""

enter image description here

  1. The array variable appears initially as a single value. Just change "var" to "var[start...end] and the array values appear.

enter image description here

enter image description here

查看更多
劫难
4楼-- · 2020-03-09 09:22

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.

查看更多
唯我独甜
5楼-- · 2020-03-09 09:24

Two dimensional arrays sometimes cannot be displayed that way. There is a work-around. First, declare a two-dimensional array as a one-dimensional array like this:

    int width = 3;
    int height = 4;
    int* array2D = new int [width*height];
    int x,y;
    for(x=width-1;x>-1;x--)
        for(y=height-1;y>-1;y--)
            array2D[x*height + y] = -1; // mark a breakpoint here!
    // add to expression evaluator: (int[3][4]) *array2D
    delete [] array2D;

Then add (int[3][4]) *array2D to the expression evaluator. Unfortunately you have to index the array your self, but you can write a special-purpose inline function or use another encapsulation method to make it slightly cleaner.

查看更多
男人必须洒脱
6楼-- · 2020-03-09 09:30

Just right-click on your variable, and choose Change Value Display Format and check Array of 100 items.

查看更多
登录 后发表回答