Получение количества каждого слова в отсортированном массиве - PullRequest
0 голосов
/ 09 октября 2018

Цель следующего сегмента кода - взять отсортированный массив строк и посчитать, сколько в нем каждого слова.

Эта информация затем помещается в структуру, которая называется ReduNode, которая содержит строку и счетчик для данной строки.

Структуры ReduceNode помещаются в другой массив.

Как только все слова и их количество найдены и помещены в промежуточный массив, они вставляются в глобальный массив структур reduNode.

Этот метод вызывается потоками, поэтому я сохраняю результаты в глобальном массиве.

Каждый раз, когда я запускаю эту часть программы, я получаю ошибку сегмента.

Я предполагаю, что у меня есть доступ к массиву за пределами границ, но у меня возникли проблемы с сужением, где я делаю это.

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;

}

reduNode определен как

typedef struct reduceNode
{
    int count;
    char *word;
    int num;
} reduceNode;

sortedArray объявлен глобально как

reduceNode **sortedArray;

и позже инициализирован как

sortedArray = (reduceNode **)calloc(1,sizeof(reduceNode*)*inputCount);

Вводcount - это количество слов, которые считываются в программу

Примером ввода может быть массив: [альфа, альфа, браво, чарли, чарли, чарли, дельта].Ожидаемый результат будет [альфа 2, браво 1, Чарли 3, дельта 1].

1 Ответ

0 голосов
/ 09 октября 2018

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.

...