이 작업 시도 중 ... GDB는 인덱스가 어떤 이유로 꺼져 있음을 나타냅니다. 두 가지 방법으로 정렬해야하는 주로 population (int) 및 name (string)을 포함하는 Record라는 하위 클래스의 벡터를 사용하고 있습니다. bt는 isSmaller() 함수의 'if'문인 27 행의 널 포인터를 나타냅니다. 이 함수는 동일한 프로그램의 삽입 정렬 코드에서는 완벽하게 작동하지만 병합 정렬에서는 작동하지 않으므로 병합 정렬 코드에 무엇이 잘못된 것인지 궁금합니다. 제발 조언. 알고리즘에 문제가 있습니까?Segfault C++에서 정렬 코드
BT는 다음을 반환 : 당신은 널 포인터를 가지고, 27, 즉 :
#0 0x0000000000403160 in CensusData::isSmaller (this=0x7fffffffdd10, type=0, r1=0x609590, r2=0x0) at CensusDataSorts.cpp:27
#1 0x0000000000403510 in CensusData::mergeIt (this=0x7fffffffdd10, type=0, list=..., p=0, q=1, r=2) at CensusDataSorts.cpp:96
#2 0x0000000000403347 in CensusData::mergeSortIt (this=0x7fffffffdd10, type=0, list=..., p=0, r=2) at CensusDataSorts.cpp:70
#3 0x0000000000403645 in CensusData::mergeSort (this=0x7fffffffdd10, type=0) at CensusDataSorts.cpp:113
#4 0x0000000000401a50 in runMergeSorts (fp=...) at CensusSort.cpp:106
#5 0x0000000000401e6f in main (argc=2, argv=0x7fffffffe068) at CensusSort.cpp:174
코드 CensusDataSorts.cpp에서)
bool CensusData::isSmaller(int type, Record* r1, Record* r2)
{
if(type==POPULATION)
{
if(r1->population <= r2->population)
return true;
}
else if(type==NAME)
{
if(*(r1->city) <= *(r2->city))
return true;
}
return false;
}
void CensusData::mergeSortIt(int type, vector<Record*>& list, int p, int r)
{
if(p < r)
{
int q = floor((p+r)/2);
mergeSortIt(type,list,p,q);
mergeSortIt(type,list,q+1,r);
mergeIt(type,list,p,q,r);
}
}
void CensusData::mergeIt(int type, vector<Record*>& list, int p, int q, int r)
{
int n1=q-p+1;
int n2=r-q;
int i,j;
vector<Record*> L(n1,0);
vector<Record*> R(n2,0);
for(i=0; i<n1; i++)
L[i]=list[p+i];
for(j=0; j<n2; j++)
R[j]=list[q+j+1];
i=0;
j=0;
for(int k=p; k<=r; k++)
{
if(isSmaller(type,L[i],R[j]))
{
list[k]=L[i];
i++;
}
else
{
list[k]=R[j];
j++;
}
}
}
void CensusData::mergeSort(int type)
{
mergeSortIt(type, data, 0, data.size()-1);
}
GDB에서 이미 디버깅 중이므로 함수 호출 백 트레이스를 사용 하시겠습니까? 그리고 표시된 코드에서 충돌이 발생한 지점을 지적하십시오. 또한 충돌이 발생했을 때 인덱스뿐만 아니라 관련된 벡터의 크기도 추가하십시오. –
call stack please .. – 51k
GDB에서 bt 유형을 의미하고 출력을 게시 하시겠습니까? 프로그램 수신 신호 SIGSEGV, 세그먼트 오류. CensusData :: isSmaller에서 0x0000000000403160은 CensusDataSorts.cpp에서 (이 0x7fffffffdd10, 유형 = 0, R1 = 0x609590, R2 =이 0x0 =) : (R1-> 인구 <= r2-> 인구) – Prasad