Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C array size via gdb

Tags:

c

gdb

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

like image 360
bph Avatar asked Jan 10 '12 18:01

bph


2 Answers

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

For int array[5],

(gdb) ptype array
type = int [5]
like image 56
hrkzmnm Avatar answered Nov 09 '22 09:11

hrkzmnm


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.

like image 35
Kevin Avatar answered Nov 09 '22 11:11

Kevin