OrenIshShalom
OrenIshShalom

Reputation: 7152

Accessing vector of vectors in gdb

I have some executable without any knowledge on the compilation flags used to built it (optimization, debug info, etc.)

Inside it, I have a function with an input variable called values (passed by reference):

void F(std::vector<std::vector<unsigned char> > &values) { // ... }

I use gdb to print values to console:

(gdb) print values

And I get this:

$15 = std::vector of length 1, capacity 1 = {std::vector of length 4, capacity 4 = {0 '\000', 0 '\000', 50 '2', 0 '\000'}}

That is, a vector of size 1, holding a vector of size 4.

When I try to access the inner size 4 vector with gdb:

(gdb) print values[0]

I get this:

Could not find operator[].

However, when I compile and debug a simple "hello vector of vectors world", with no optimization, and -ggdb flag:

(gdb) print values

I get this:

$2 = std::vector of length 1, capacity 1 = {std::vector of length 3, capacity 3 = {48 '0', 49 '1', 50 '2'}}

And when I try to access the inner vector with:

(gdb) print values[0]

everything is OK:

$3 = std::vector of length 3, capacity 3 = {48 '0', 49 '1', 50 '2'}

Could this be a matter of optimization?? debug info??

Any help is very much appreciated ... Thanks!

Upvotes: 0

Views: 951

Answers (1)

Employed Russian
Employed Russian

Reputation: 213754

Could this be a matter of optimization??

Yes.

When you print values[0], GDB attempts to find a function to call, here std::vector<unsigned char>::operator[](size_t). In the non-optimized case, GDB does find it, calls it, and prints the result. In the optimized case, the function has been inlined, and so there is no externally callable function in the executable that GDB could use; hence Could not find operator[] error.

Upvotes: 1

Related Questions