51

Right now it is only showing the first element of the array but I want a visual of all the elements in the array. I think Clion is using GDB.

EDIT: I am referring specifically to arrays on the heap. Arrays on the stack can be visualised.

Chen Li
  • 4,824
  • 3
  • 28
  • 55
Embedded_Mugs
  • 2,132
  • 3
  • 21
  • 33
  • 1
    There's no such feature in CLion right now, unfortunately. Here's the corresponding feature request though: [CPP-6550 'Show as array' for pointer variables](https://youtrack.jetbrains.com/issue/CPP-6550), please upvote if that's the case for you. – Eldar Abusalimov Oct 31 '16 at 09:43
  • 1
    FTR: You vote up by clicking the *tiny* thumb-up icon on the right side of the page. – cubuspl42 Nov 02 '16 at 12:56

6 Answers6

67

The answer by cubuspl42 works for GDB. But if you're on a Mac using LLDB as your debugger, the correct method is

(MyType(*)[128])myArray

Hope this helps!

Community
  • 1
  • 1
59

Unfortunately, CLion doesn't currently support such feature. As suggested by JetBrains employee, you can use a workaround. In Evaluate / Watches window use the following expression:

(MyType[128])myArray

You can use arbitrary array size; whatever works for you.

If you array is stored in void * variable, you need to do something more tricky:

(MyType[128])*(char*)myArray
glennsl
  • 28,186
  • 12
  • 57
  • 75
cubuspl42
  • 7,833
  • 4
  • 41
  • 65
  • 12
    Note (MyType[128])myArray works for LLDB. For GDB, use (MyType[128])*myArray to have correct results. – Da Teng Jun 19 '19 at 00:19
  • @cubuspl42, thank you for your solution. `void *` works for any type – bem22 Oct 27 '19 at 22:02
  • `(char *[10])*argv` If you are trying to watch the program's command line arguments. `argv` itself is null terminated. You will see environment variables after the null, or maybe garbage on non unixy systems. – bigh_29 Feb 03 '21 at 20:51
  • This unfortunately didn't work for in Linux with CLion, with/out asterisk; I think you might have to do a few CMAKE flags first or at least built with optimisation. – stucash Mar 03 '23 at 14:10
6

Any syntax understood by the underlying debugger should work, actually. In the case of GDB, for example, you could use *array@size, where array can be any pointer expression and size can be any (positive) integer expression, and both can include variables, function calls, registers, anything that GDB understands. Something like this would be valid, for example:

*((int*)$rsp - 0x100)@get_size(data)
EvgEnZh
  • 699
  • 8
  • 13
2

I had the same problem today, but instead, I had an array of pointers;

pthread_t** pthreads = (pthread_t**) malloc(//malloc args)
thread_count = 0;

while(thread_count < 10) {
    pthread_t* myThread = (pthread_t*) malloc(//malloc args)
    pthreads[thread_count] = myThread;
    thread_count++;
}

I had trouble seeing the allocation of this memory in CLion gdb because it looked at a pointer to a pointer.

I solved this by targetting the first element of my array (pthreads[0]) and then looking at the next n elements from there.

To do this you need to cast the type (pthread_t*[]) and then use the target memory, which is pthreads[0] (i.e first element)

Note: I used calloc with 0 to set my pthreads array. This photo shows how memory was allocated correctly at position 0 in the CLion debugger.

I made this post because none of the posts above led me to the conclusion that I wrote here.

Example: clion gdb feature

bem22
  • 276
  • 1
  • 14
1

You can use template and reference:

template<int N>
void foo1(int (&arr)[N])
{
    ...
}

If you want to pass the array to other function, the passed function should also use template and reference for array:

template<int N>
void foo2(int (&arr)[N])
{
    ...
}
template<int N>
void foo1(int (&arr)[N])
{
    foo2(arr);
}

This method allows you to see the entire contents of an int array in clion

Chen Li
  • 4,824
  • 3
  • 28
  • 55
1

Note that as of version 2021.3, you can now view pointers as arrays by right-clicking on the specific variable in the variable viewer and selecting "View as Array...": https://www.jetbrains.com/help/clion/examining-suspended-program.html#view-as-array

Helmster
  • 15
  • 5
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 06 '23 at 00:20