0

I have an object a of type vector<char>. I know it is 10 elements long. But, when I do p a[5] in gdb, I get an error saying gdb could not resolve symbol operator[].

void foo()
{
    vector<char> a (10, 10); // put a breakpoint here.
}

Could you please suggest me a way to print the value at index in gdb? Right now, I did something really lame -

char c1=a[1]; char c2 = a[2]; char c3 = a[3]; // ... so on and forth

Can I trust the values in c1, c2, etc

P.S. I have built my code with DEBUG symbols.

0

2 Answers 2

2

A vector is not the same as an array or a pointer. A vector typically has a pointer to the start, which you can use to access elements. Try something like this

p *(a._M_impl._M_start+5)

Here I'm accessing the pointer to the start of the array, adding 5 to get the element at index 5.

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

1 Comment

Note that a._M_impl._M_start[5] works as well, and it's possible to use tab-completion.
1

This works without issues with gdb 7.11.1

(gdb) b main
Breakpoint 1 at 0x400bd3: file t.C, line 6.
(gdb) run
Starting program: /tmp/t 
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.23.1-8.fc24.x86_64

Breakpoint 1, main () at t.C:6
6       std::vector<char> a={'a','b','c','d','e','f'};
Missing separate debuginfos, use: dnf debuginfo-install libgcc-6.1.1-3.fc24.x86_64 libstdc++-6.1.1-3.fc24.x86_64
(gdb) n
8       std::cout << "Ok" << std::endl;
(gdb) p a
$1 = std::vector of length 6, capacity 6 = {97 'a', 98 'b', 99 'c', 100 'd', 
  101 'e', 102 'f'}
(gdb) p a[2]
$2 = 99 'c'

The answer here is to update to the current version of gcc and gdb.

2 Comments

Did you write a custom pretty printer?
No. Stock gdb, on Fedora 24. No customizations.

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.