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.
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.
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!
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
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)
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.
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
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