View Array contents in Qt Creator debugger
Asked Answered
I

6

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.

Irwinirwinn answered 23/9, 2011 at 19:25 Comment(0)
H
15

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

Or, *myArray@10 instead of myArray@10

Hindward answered 16/12, 2013 at 19:20 Comment(0)
L
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.

Larock answered 24/9, 2011 at 17:57 Comment(0)
D
8

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

Desiderative answered 26/8, 2016 at 9:8 Comment(0)
B
1

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

Banas answered 9/10, 2018 at 18:24 Comment(0)
W
1

Two dimensional arrays sometimes cannot be displayed by the currently top voted answer. 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.

Wicklund answered 3/12, 2018 at 21:51 Comment(0)
J
0

In my case, I had a void *values that was an array of doubles. The size of array was saved to another variable num_values.

I viewed the contents of this array by first getting the value of num_values (eg 50), and then using this expression to actually view the values:

(*((double *)values))@50

or using this (equivalent but simpler) expression:

*(double *)values@50

Explanation:

  1. cast values variable from void * to double *
  2. dereference that pointer to get the first value (as double)
  3. get 50 values instead of 1.
Jaejaeger answered 22/7 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.