1

Let's say I have an array of pointers:

struct S {int x;};
S *arr[3] = {new S{1}, new S{2}, new S{3}};

I want to make gdb dereference and print each element of the array.

Printing one element is simple:

(gdb) p *arr[0]                                                                                            
$1 = {x = 1}

but I want to print every element in the array in this fashion.

I tried p arr, but it outputs the pointers, rather than the objects they point to:

(gdb) p arr
$1 = {0x613c20, 0x613c40, 0x613c60}
0

1 Answer 1

2

I have not found better than that:

(gdb) set $i=0
(gdb) p *arr[$i++]
$102 = {x = 1}
(gdb) 
$103 = {x = 2}
(gdb) 
$104 = {x = 3}
(gdb)

maybe it can help.

note: simply press return to show the next value.

Sign up to request clarification or add additional context in comments.

2 Comments

can i use for in gdb?
thank you,i have sovled all problem,because i can use while in gdb,and it can comletely sovle the problem,although its little complex

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.