以下代码段的目标是获取一个排序的字符串数组,并计算每个单词中有多少个。
然后将该信息放入名为reduceNode的结构中,该结构包含一个字符串和给定字符串的计数。
reduceNode结构放入另一个数组。
一旦找到所有单词及其计数,并将它们放入中间数组,它们就会插入到reduceNode结构的全局数组中。
该方法被线程调用,这就是为什么我将结果存储到全局数组中的原因。
每当我运行程序的这一部分时,我都会遇到段错误。
我假设我正在访问数组,但我在缩小范围时遇到了麻烦。
void* reduce(void* num) //Reduce function
{
int index = *(int*)num;
int curSize = 0; //Size of the current linked list
struct HashNode *head = HashTable[index]; //Get the head of the linked list from the hashtable
struct HashNode *linkedList = head; //Pointer to the head to traverse the linked list
while(linkedList != NULL) //Gets the size of the current linked list
{
curSize++;
linkedList = linkedList->next;
}
linkedList = head;
int linkedListTraverse = 0; //Array index for each linked list node
int numSort[curSize];
char* wordSort[curSize];
while(linkedList != NULL)
{
if(app == 1)
numSort[linkedListTraverse] = linkedList->num; //Copy the data from the linked list into an array
else
{
wordSort[linkedListTraverse] = (char*) malloc(sizeof(linkedList->string));
strcpy(wordSort[linkedListTraverse],linkedList->string); //Copy the data from the linked list into an array
}
linkedList = linkedList->next;
linkedListTraverse++;
}
if(app == 1)
{
qsort(numSort, curSize, sizeof(int), numCmpFunc); //Sort the current node
int i, j = 0;
reduceNode* numSortArray[curSize];
reduceNode* curNum;
for(i = 0; i < curSize; i++)
{
curNum = (reduceNode*) malloc(sizeof(reduceNode));
curNum->num = numSort[i];
numSortArray[i] = curNum;
}
i = 0;
while(sortedArray[i] != NULL)
{
i++;
}
for(j = 0; j < curSize; j++, i++)
{
sortedArray[i] = numSortArray[j];
}
return (void*) 0;
}
else
{
int i = 0;
while(i < curSize) //Convert all of the words to lowercase
{
char* str = wordSort[i];
char *p;
for (p = str; *p != '\0'; p++)
*p = (char)tolower(*p);
i++;
}
qsort(wordSort, curSize, sizeof(char*), stringCmpFunc); //Sort the current node
}
int curWordIndex = 0; //Exclusively for wordcount
int checkWordIndex = 1;
int curArrayIndex = 0;
reduceNode *curWord;
reduceNode* wordCountArray[curSize];
while(curWordIndex < curSize)
{
curWord = malloc(sizeof(reduceNode));
curWord->word = wordSort[curWordIndex]; //Set the word
curWord->count = 1; //Start the count out at 1
while(strcmp(wordSort[curWordIndex], wordSort[checkWordIndex]) == 0) //While the two words are equal
{
checkWordIndex++; //Advance the leading index check
curWord->count++;
if(checkWordIndex >= curSize) //If the leading index goes beyond the array bounds
{
break;
}
}
if(checkWordIndex <= curSize)
{
curWordIndex = checkWordIndex;
checkWordIndex = curWordIndex + 1;
}
else if(checkWordIndex >= curSize) //If the leading index goes beyond the array bounds
{
if(strcmp(curWord->word, wordSort[curWordIndex]) != 0)
{
curWord->word = wordSort[curWordIndex]; //Set the word
curWord->count = 1; //Start the count out at 1
curArrayIndex++;
wordCountArray[curArrayIndex] = curWord;
}
else
{
wordCountArray[curArrayIndex] = curWord;
curArrayIndex++;
}
break;
}
wordCountArray[curArrayIndex] = curWord;
curWord = NULL;
curArrayIndex++;
}
int i,j = 0;
while(sortedArray[i] != NULL)
{
i++;
}
for(j = 0; j < curSize; j++, i++)
{
sortedArray[i] = wordCountArray[j];
}
return (void*) 0;
}
reduceNode定义为
typedef struct reduceNode
{
int count;
char *word;
int num;
} reduceNode;
sortedArray全局声明为
reduceNode **sortedArray;
,然后初始化为
sortedArray = (reduceNode **)calloc(1,sizeof(reduceNode*)*inputCount);
输入计数是读入程序的单词数
一个示例输入将是一个数组:[alpha,alpha,bravo,charlie,charlie,charlie,delta]。预期结果将是[alpha 2,bravo 1,charlie 3,delta 1]。
答案 0 :(得分:1)
1。
您checkWordIndex
正好到达curSize
,strcmp(wordSort[curWordIndex], wordSort[checkWordIndex]
会超出范围。我建议打印调试标记。
if(checkWordIndex < curSize)
{
curWordIndex = checkWordIndex;
checkWordIndex = curWordIndex + 1;
}
此代码仍将导致checkWordIndex == curSize
2。 您分配了新的内存,记住要释放它。
3。 对于C中的线程安全查找互斥锁。
我建议仅使用一个索引并像
那样进行迭代while(index < cursize-1)
{
...
++index;
}
您的拳头标记是index
,而您的第二个标记是index+1
。