Reputation: 915
I just debuging c++ program and I have value
vector<bool> B(n, 0);
My question is how to print it value in gdb console? Because eclipse IDE is not too smart to present it in redable format.
When I had integer vector I can do something like:
printf "%x %x %x", B[0], B[1], B[2]...
so I was able to read this vector. But with boolean is hard because printf didn't have format to print it, also I can't type in console p B[0]; p B[1] ...
So maybe anybody has idea how to debug stuff like this?
Upvotes: 3
Views: 5309
Reputation: 38939
bitset
is a far better option than vector<bool>
.
Among other reasons because it supports stream operators, so you can just stream this thing out.
This of course assumes that the data type is under your control.
With printf
you can use bitset
's to_string
EDIT in response to Enrico:
In the event that a bitset
cannot be used it is possible to print vector<bool>
elements from GDB, but how a vector<bool>
is implemented in GCC needs to be understood. vector<bool>
's index operator is not returning a bool
but a vector<bool>::reference
which is not a bool
but provides a conversion operator to a bool
.
In GCC this reference
has a _M_p
member which is a pointer to the storage allocated for the vector<bool>
class and a _M_mask
member which can be used to mask all but the indexed bit. The combination of these two yields either a zero or non-zero value. Zero means the bit is unset and a non-zero means the bit is set.
For a vector<bool> foo
you could print this combination with the GDB command:
print *(foo[i]._M_p) & foo[i]._M_mask
Upvotes: 5