C array size via gdb

独自空忆成欢 提交于 2021-02-06 10:12:33

问题


I know you can print an array in gdb , e.g.

(gdb) p *array@10

Is there a gdb command that can tell you its length, e.g. a handy shortcut to typing something like:

(gdb) p sizeof(array)/sizeof(int)

In the case where the array has been defined at compile time and you want to check it


回答1:


You may use ptype to know the type of a symbol.

For int array[5],

(gdb) ptype array
type = int [5]



回答2:


If it's actually defined as an array, e.g.

int array[5];

Then yes, you can use what you wrote, although a better and more general way is:

(gdb) p sizeof(array)/sizeof(*array)

This doesn't assume the type of the array.

If the variable is defined as a pointer, then no.



来源:https://stackoverflow.com/questions/8808448/c-array-size-via-gdb

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