开发者

C- Possible stack overflow from a function

开发者 https://www.devze.com 2023-03-04 13:02 出处:网络
I\'m writing a HashTable program where the HashTable is full of LinkedLists which counts the word frequencies of a file. In the final implementation of the program, the user is allowed to choose what

I'm writing a HashTable program where the HashTable is full of LinkedLists which counts the word frequencies of a file. In the final implementation of the program, the user is allowed to choose what the chain size of the linked list via a command line argument with any other command line arguments assumed to be files that need to be processed.

Right now I am getting a stack over flow problem from my search function to determine whether the word is already in the table or not and I'm not sure how to fix it. I valgrind the program it said it was a possible over flow

Before I add another node to the hashtable I call a method that returns 1 if the word is found and 0 if it is not found. Here is the call:

 inFile = searchFile(table, data);

table is the HashTable and data is the word

searchFile which calls search which is part of my List class

 int searchFile(HashTablePtr table, char *data) {
     ListPtr tempList;
     HashObjectPtr obj;
     int i;

     for (i = 0; i < table->size; i++) {
         tempList = table->table[i];
         if (tempList->size > 0) {
             obj = search(table->table[i], data);
            if (obj != NULL) {
                obj->frequency++;开发者_如何学C
                return 1;
            }
        }

    }
    return 0;
}

List search function

 HashObjectPtr search(ListPtr list, char *data) {
     int num = list->size;
     int i;

     NodePtr tempNode;
     HashObjectPtr tempObj;

     tempNode = list->head;

     for (i = 0; i < num; i++) {
         tempObj = tempNode->HashObject;
         while (tempNode != NULL) {
             if (strcmp((char *) tempObj->obj, data) == 0) {
                return tempObj;
             } else {
                tempNode = tempNode->next;
             }
         }
     }

    return NULL;
}

I've used the HashObjectPtr search function before in a previous implementation of this program without any problems and I'm not sure why it would be creating an overflow now.

HashObject

 struct hashobject {
     int frequency;
     void *obj;

     char (*toString)(void *obj);
 };

HashTable

 struct hashtable {
      int size;
      ListPtr *table;
 };

Lise

 struct list {
     int size;
     NodePtr head;
     NodePtr tail;
 };

I'm not entirely sure that it is a stack overflow as all valgrind really shows is that a few words have gone in and then I get a segmentation fault error. I have ran it through the debugger but it fails at my search function after going through about 12 iterations of the for loop.


One detail, I don't know if it will solve your problem. In your original loop:

     tempObj = tempNode->HashObject;
     while (tempNode != NULL) {
         if (strcmp((char *) tempObj->obj, data) == 0) {
            return tempObj;
         } else {
            tempNode = tempNode->next;
         }
     }

You will assign tempObj to the first tempNode. Then you will loop through all the nodes. In addition to not finding anything except the first node, it will not work when the list is empty.

Instead, place it side the loop:

     while (tempNode != NULL) {
         tempObj = tempNode->HashObject;
         if (strcmp((char *) tempObj->obj, data) == 0) {
            return tempObj;
         } else {
            tempNode = tempNode->next;
         }
     }
0

精彩评论

暂无评论...
验证码 换一张
取 消