Printing Elements of a C Vector in GDB
When debugging C code in GDB, you may need to examine the contents of a vector. To do so with a vector of integers (std::vector
Printing the Entire Vector:
print *(myVector._M_impl._M_start)@myVector.size()
This expression accesses the pointer to the internal array of the vector (_M_impl._M_start) and prints the specified number of elements (myVector.size()).
Printing Only the First N Elements:
print *(myVector._M_impl._M_start)@N
Replace N with the number of elements you want to print.
Explanation:
Note: This approach may vary depending on your compiler version. For GCC 4.1.2, this method has been tested to work effectively.
The above is the detailed content of How to Print Elements of a C Vector in GDB?. For more information, please follow other related articles on the PHP Chinese website!