2012-08-12 3 views
0

나는 프로그램에서지도를 사용하여 희소 배열을 압축 배열로 변환합니다.표준지도 세분화 오류 제공

map<int, int> m_mCustIds; 
map<int, int>::iterator itr; 

for (i=0; i<m_nRatingCount; i++) 
{ 
Data* rating = m_aRatings + i; 
itr = m_mCustIds.find(rating->CustId); 
if (itr == m_mCustIds.end()) 
    { 
     cid = 1 + (int)m_mCustIds.size(); 
    } 
    else 
    { 
     cid = itr->second; 
    } 
// using cid in other data structures 
} 

고정 된 번호 (100498277)의 항목을 처리 한 후 세그먼트 오류가 발생합니다.

GDB Output 
Program received signal SIGSEGV, Segmentation fault. 
0x0000000000401ea0 in std::less<int>::operator() (this=0x2aab0adf5ed8, [email protected], [email protected]) 
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:227 
227   { return __x < __y; } 

BT는

==18544== Invalid read of size 4 
==18544== at 0x401EA0: std::less<int>::operator()(int const&, int const&) const (stl_function.h:227) 
==18544== by 0x40218E: std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::find(int const&) (stl_tree.h:1317) 
==18544== by 0x402242: std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::find(int const&) (stl_map.h:534) 
==18544== by 0x401B17: Engine::CalcMetrics() (At.cpp:204) 
==18544== by 0x401DA2: main (At.cpp:141) 
==18544== Address 0x22 is not stack'd, malloc'd or (recently) free'd 

Valgrind의 또한 프로그램 가능한 시스템 메모리의 10 %를 사용하여지도 의 다른 기능의 데이터 possilbe 손실을보고

#0 0x0000000000401ea0 in std::less<int>::operator() (this=0x2aab0adf5ed8, [email protected], [email protected]) 
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:227 
#1 0x000000000040218f in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::find (this=0x2aab0adf5ed8, [email protected]) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:1317 
#2 0x0000000000402243 in std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::find (this=0x2aab0adf5ed8, [email protected]) 
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:534 
#3 0x0000000000401b18 in Engine::CalcMetrics (this=0x2aaaaaad9010) at At.cpp:204 
#4 0x0000000000401da3 in main() at At.cpp:141 

Valgrind의 출력을 나타낸다.

+0

어떤 유형이'i'입니까? 당신은'Data *'형의 어떤 것에 그것을 추가하고 그것을'm_nRatingCount'와 비교합니다. – Shep

+0

i는 int 유형이고 m_nRatingCount는 – siddhu323

+0

입니다. 204 행은 어느 것입니까? –

답변

2

당신은 itr->second에 액세스하기 전에 itr를 초기화하지 않았다 도와주세요. 실제로 itr에 법적 가치를 지정하지 않았습니다.

+0

포맷 중에 실수로이 행을 삭제했습니다. 죄송합니다. 나는 그것을 지금 편집했다. itr = m_mCustIds.find (등급 -> CustId); – siddhu323

+0

@ siddhu323 : 그럼'm_aRatings'을 어떻게 얻습니까? 그 유형은 무엇입니까? '등급'이 합법적 인 주소를 가리키고 있습니까? – timrau

+0

m_aRatings는 Data 형식의 배열입니다. 데이터는 다음 정의가있는 구조입니다. 구조체 데이터 { int CustId; int MovieId; int 등급; float Cache; }; 많은 수의 항목에 대해 올바르게 작동하기 때문에 법적 주소를 가리킨다 고 생각합니다. – siddhu323