How to “watch” a C++ dynamic array using gdb?

半腔热情 提交于 2020-01-12 06:53:46

问题


Consider the following example:

int size = 10, *kk = new int[size];

for (int i = 0; i < size; i++) {
    kk[i] = i;
}

delete [] kk;

How can I add a watch for the whole array? I can add a watch one by one (kk[0],kk[1]...), but since I know the array's length is there a way to do it automatically? I mean something like kk[0..size-1] or so.

I'm using NetBeans IDE together with cygwin g++ and gdb.


回答1:


Try display *kk@<size> From the doc for the print command:

@ is a binary operator for treating consecutive data objects anywhere in memory as an array. FOO@NUM gives an array whose first element is FOO, whose second element is stored in the space following where FOO is stored, etc. FOO must be an expression whose value resides in memory.



来源:https://stackoverflow.com/questions/1651682/how-to-watch-a-c-dynamic-array-using-gdb

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