16

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

2 Answers 2

22

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

For int array[5],

(gdb) ptype array
type = int [5]
Sign up to request clarification or add additional context in comments.

Comments

15

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.

2 Comments

good point, that is more generic - i thought there may be some gdb shortcut for that? or possibly you can define your own somehow?
@Hiett Well, if you just print the array, it'll print all the values. Beyond that, I don't know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.