Printing Vector Elements in C via GDB
When debugging C code in GDB, examining the contents of a std::vector can be challenging. For instance, consider a std::vector
In GCC 4.1.2, the solution involves accessing the vector's internal pointer, myVector._M_impl._M_start, which points to the array holding the vector's elements.
To print the entire vector, use:
print *(myVector._M_impl._M_start)@myVector.size()
To print only the first N elements, modify it to:
print *(myVector._M_impl._M_start)@N
Reasoning
This approach leverages the GDB command to print N elements of an array starting at a given pointer. In this case, the pointer is myVector._M_impl._M_start, and we specify the number of elements to print using myVector.size() or the desired count N.
While this approach is applicable to GCC 4.1.2, it might vary depending on your compiler version. So, for other versions, consulting the relevant documentation is recommended.
The above is the detailed content of How to Print Vector Elements in C Using GDB?. For more information, please follow other related articles on the PHP Chinese website!