이 코드를 사용하여 lfind 호출에서 세그먼트 화 오류가 발생합니다. CVector는 elems라는 배열을 가진 구조체입니다. CVectorCreate 및 CVectorAppend 함수가 작동하는 것을 알고 있습니다. 첫 번째 블록은 클래스의 일부로 제공되며 변경할 수없는 테스트 코드이고 두 번째 블록은 필자가 작성한 함수 호출입니다. 누군가 내 문제를 식별하도록 도와 줄 수 있습니까? 감사! bsearch
달리lfind에서 분할 오류가 발생 했습니까?
char *jumbled = "xatmpdvyhglzjrknicoqsbuewf";
CVector *cv = CVectorCreate(sizeof(char), 4, NULL);
for (int i = 0; i < strlen(jumbled); i++)
CVectorAppend(cv, &jumbled[i]);
printf("\nDoing linear searches on unsorted cvector.\n");
char ch = '*';
Verify(0, CVectorSearch(cv, &jumbled[0], CmpCharElem, 0, false), "Linear search");
int CVectorSearch(const CVector *cv, const void *key, CVectorCmpElemFn cmpfn, int startIndex, bool isSorted)
{
assert(startIndex >= 0 && startIndex <= cv->logicalLength);
void *found = NULL;
if (isSorted == true) {
found = bsearch(key, (char *)(cv->elems) + (startIndex * cv->elemSize),
cv->logicalLength, cv->elemSize, cmpfn);
} else {
found = lfind(key, (char *)(cv->elems) + (startIndex * cv->elemSize), cv->logicalLength, cv->elemSize, cmpfn);
}
'& 뒤죽박죽 [0]'- 그냥 jumbled''에,이 포인터 ... 멀리 세그먼트 폴트를 제거하는 등의 일을 –