2017-11-18 20 views
0

를 손상되었습니다비주얼 스튜디오 - 변수 '임시'주위에 스택 내가 cstrings의 배열을 정렬 빠른 정렬 알고리즘에 대한 요소를 분할하는 기능을 작성했습니다

void partition(char words[][MAXWORDLEN + 1], int start, int end, int& partitionIndex) { 
char pivot[MAXWORDLEN + 1]; //choose last element to be the pivot 
strcpy(pivot, words[end]); 
partitionIndex = start; //partition index is initalized to the first index 
for (int i = start; i < end; ++i) { //iterate through the array 
    if (strcmp(words[i], words[end]) < 0) { 
     char temp[MAXWORDLEN]; 
     strcpy(temp, words[i]); //swap the element with the element corresponding to the partition index 
     strcpy(words[i], words[partitionIndex]); 
     strcpy(words[partitionIndex], temp); 
     partitionIndex++; 
    } 
} 
cout << end << endl; 
char temp[MAXWORDLEN + 1]; 
strcpy(temp, words[end]); 
strcpy(words[end], words[partitionIndex]); 
strcpy(words[partitionIndex], temp); 

}

그러나 프로그램을 실행하면 런타임 검사 오류가 발생합니다. MAXWORDLENGTH는 6이며 배열의 모든 단어는 4-6 자 사이에 있습니다. 피벗 배열이 있기 때문에

char temp[MAXWORDLEN + 1]; 

이에

char temp[MAXWORDLEN]; 

: 그래서 변수 임시 인덱스 partitionIndex에서 말을

+0

'\ 0'에 대한 임시 [MAXWORDLEN + 1]? – coderredoc

+0

@coderredoc 그래, 그래서 나는 c 함수를 사용할 수있다. –

+0

'if' 문 안에서도 똑같이 만든다. 그것이 내가 의미하는 바이다. '임시 [MAXWORDLEN +1]' – coderredoc

답변

1

변경이를 복사 할 듯없는 이유 혼란 스러워요 이 크기도. temp 크기 6,은 6 자했다 포함하는 단어 때


은 그래서, null 종결는 복사가 실패하고 정의되지 않은 동작을 호출하는 것을 의미 덮어 쓸 것입니다. 복사를 통해 어떤 가비지 값이 대상 배열에 쓰여지는지는 알 수 없습니다.

+0

좋은 @coderredoc ,,하지만이 질문에 대한 대답이라고 생각합니다. – gsamaras