In Clion's debugger, how do I show the entire contents of an int array

流过昼夜 提交于 2020-01-30 14:18:07

问题


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.


回答1:


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

Please upvote this issue, to increase the chance of getting a real solution. You do this by clicking the tiny thumb-up icon on the right side of the page.




回答2:


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!




回答3:


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)



回答4:


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




回答5:


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:



来源:https://stackoverflow.com/questions/40327089/in-clions-debugger-how-do-i-show-the-entire-contents-of-an-int-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!