简体   繁体   中英

gdb how to print vector<bool> value

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM