2011-11-23 2 views
19

클래스의 일부로 std :: vector가 있는데, 여기에는 사용자 정의 유형이 들어 있습니다. 그 내용은 프로그램의 어딘가에서 신비하게 바뀐 것 같습니다. 이런 일이 어디에서 일어나는지 알아 내려고 애쓰는 데 어려움을 겪고 있습니다.gdb에서 C++ std :: vector의 크기를 "보는"방법은 무엇입니까?

gdb에서 std :: vector의 내용 (또는 크기)을 "감시"할 수있는 방법이 있습니까?

감사합니다.

+0

분리 및 크기 증가가 발생하고 자세한 내용의 조금을 제공하는 곳 사이의 코드 부분을 정의 할 수 있습니까? std :: vector는 템플리트 구현 만해야하므로 소스의 액세스가 가능해야하므로 vector의 allocate() 내에 중단 점을 배치 할 수 있습니다. –

+0

크기 증가가 어디에서 발생하는지 알지만 size 함수가 다른 함수에 의해 나중에 호출 될 때 크기를 0으로보고하고 어디에서 알아낼 수없는 것처럼 보입니다. 벡터에는 명시 적 new/delete를 수행하지 않는 데이터 구조에 대한 포인터가 들어 있습니다. – endbegin

+0

그러면 특정 영역에 관한 코드를 보여 주면 도움이 될 것입니다. 그렇지 않으면 (그리고 첫 번째 게시 된 답변에 대한 귀하의 의견과 관련하여) 내 부두 클 랭은 단지 침묵을 보여줍니다. o) –

답변

10

gdb에서 std :: vector의 내용 (또는 크기)을 "보는"방법이 있습니까?

GCC를 사용한다고 가정 할 때 theVector->_M_impl._M_start_M_finish에 감시 점을 설정하십시오. 다른 std :: vector 구현을 사용하는 경우 이에 따라 조정하십시오.

예 : (예를 들어, 사용자 정의 유형은 무엇인가, 자체 메모리 등을 할당 않습니다)

#include <vector> 

int main() 
{ 
    std::vector<int> v; 

    v.push_back(1); 
    v.push_back(2); 
} 

g++ -g t.cc 
gdb -q ./a.out 

Reading symbols from /tmp/a.out...done. 
(gdb) start 
Temporary breakpoint 1 at 0x40090f: file t.cc, line 5. 

Temporary breakpoint 1, main() at t.cc:5 
5  std::vector<int> v; 
(gdb) n 
7  v.push_back(1); 
(gdb) p v._M_impl._M_start 
$1 = (int *) 0x0 
(gdb) p v._M_impl._M_finish 
$2 = (int *) 0x0 
(gdb) p &v._M_impl._M_finish 
$3 = (int **) 0x7fffffffd878 
(gdb) watch *$3 
Hardware watchpoint 2: *$3 
(gdb) p &v._M_impl._M_start 
$4 = (int **) 0x7fffffffd870 
(gdb) watch *$4 
Hardware watchpoint 3: *$4 
(gdb) c 
Hardware watchpoint 3: *$4 

Old value = (int *) 0x0 
New value = (int *) 0x604010 
std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:365 
365  this->_M_impl._M_finish = __new_finish; 
(gdb) bt 
#0 std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:365 
#1 0x0000000000400a98 in std::vector<int, std::allocator<int> >::push_back (this=0x7fffffffd870, [email protected]) at /usr/include/c++/4.4/bits/stl_vector.h:741 
#2 0x0000000000400935 in main() at t.cc:7 
(gdb) c 
Hardware watchpoint 2: *$3 

Old value = (int *) 0x0 
New value = (int *) 0x604014 
std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:366 
366  this->_M_impl._M_end_of_storage = __new_start + __len; 
(gdb) bt 
#0 std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:366 
#1 0x0000000000400a98 in std::vector<int, std::allocator<int> >::push_back (this=0x7fffffffd870, [email protected]) at /usr/include/c++/4.4/bits/stl_vector.h:741 
#2 0x0000000000400935 in main() at t.cc:7 

... etc... 
+0

나는 당신의 방법을 이해할 수 없기 때문에 detial에서 당신의 방법을 설명해 주시겠습니까? 고마워. –

1

나는 http://sourceware.org/gdb/wiki/STLSupport가 도움이 될 것이라고 생각합니다.

+0

나는 그것을 시도했지만 거기에서 모을 수있는 모든 것은 예쁜 : :: 벡터를 예쁜 것으로 인쇄하는 방법이다. 비록 그것을 어떻게 볼지 모르겠다 ... – endbegin

관련 문제