简体   繁体   中英

C array being overwritten/deleted? very confused

I am working on a project in C and it is working great except for one function which seems to be overwriting my array and writing weird numbers such as 1970802352 which keeps count of word occurrences in a file

this is my header file:

#ifndef LIST_H
#define LIST_H
struct Node_{
        char* word;
        //array holding names of files word occurs in 
        char **filesIn;
        int numFilesIn;
        //array holding count of how many times word occured in file
        int* occursIn;
        struct Node_ *next;
        int isHead;
};

typedef struct Node_ Node;

int insert(char *wordToAdd, char *File);

int addOccur(Node *addedTo, char *File);

Node *createNode(char *wordToAdd, char *File);

void destroyNodes();

#endif

and this is the function that keeps overwriting the array:

Node *head;
int insert(char *wordToAdd, char *File){
        if(head == NULL){
                Node *new;
                new = createNode(wordToAdd, File);
                new->isHead = 1;
                head = new;
                return 0;
        }
        else{
                Node *trace;
                trace = head;
                char *traceWord;
                int wordSize;
                wordSize = strlen(trace->word);
                traceWord = (char*) malloc(wordSize + 1);
                strcpy(traceWord, trace->word);
                int a =strcmp(wordToAdd, traceWord);
                free(traceWord);
                if(a == 0){
                        int b = addOccur(trace, File);
                        //printf("addOccur returned %d\n", b);
                        return 0;
                }
                if(a < 0){
                        Node *Insert = createNode(wordToAdd, File);
                        trace->isHead = 0;
                        Insert->isHead = 1;
                        Insert->next = trace;
                        head = Insert;
                        return 0;
                }
                else{


                        Node *backTrace;
                        backTrace = head;

                        while(trace->next != NULL){
                                trace = trace->next;
                                traceWord = trace->word;
                                a = strcmp(wordToAdd, traceWord);
                                if(a < 0){
                                        Node* Insert = createNode(wordToAdd, File);
                                        Insert->next = trace;
                                        backTrace->next = Insert;
                                        return 0;
                                }
                                if(a == 0){
                                        addOccur(trace, File);
                                        //free(wordToAdd);
                                        return 0;
                                }
                                if(a > 0){
                                        backTrace = trace;
                                        continue;
                                }
                        }
                        Node *Insert = createNode(wordToAdd, File);
                        trace->next = Insert;
                        return 0;
                }
        }
        return 1;
}

and the other functions are:

Node* createNode(char *wordToAdd, char *File){
        Node *new;
        new =   (Node*)malloc(sizeof(Node));
        memset(new, 0, sizeof(Node));
        new->word = wordToAdd;
        char **newArray;
        newArray = (char**)malloc(sizeof(char*));
        newArray[0] = File;
        new->filesIn = newArray;
        int a[1];
        a[0] = 1;
        new->occursIn = a;
        //new->occursIn[0] = 1;
        new->numFilesIn = 1;
        return new;
}

int addOccur(Node *addedTo, char *File){

        char **fileList = addedTo->filesIn;
        char *fileCheck;
        int i = 0;
        int fileNums = addedTo->numFilesIn;
        for(i = 0; i < fileNums; i++){
                fileCheck = fileList[i];
                if(strcmp(fileCheck, File) == 0){
                        int *add1;
                        add1 = addedTo->occursIn;
                        int j = add1[i];
                        j++;
                        add1[i] = j;
                        return 0;
                }
        }

        int numberOfFilesIn;
        numberOfFilesIn = addedTo->numFilesIn;
        char **newList = (char**)malloc(sizeof(char*) * numberOfFilesIn + sizeof(char*));
        i = 0;
        char *dest;
        char *src;
for(i = 0; i < numberOfFilesIn; i++){
                src = fileList[i];
                int len;
                len = strlen(src);
                dest = (char*)malloc(sizeof(char) * (len + 1));
                strcpy(dest, src);
                newList[i] = dest;
                }
        int len2;
        len2 = strlen(File);
        newList[i] = File;
        free(fileList);
        int r = addedTo->numFilesIn;
        r++;
        addedTo->numFilesIn = r;
        addedTo->filesIn = newList;
        i = 0;
        int *g;
        g =  addedTo->occursIn;
        int count2;
        count2 = addedTo->numFilesIn;
        count2++;
        int a[count2];
        for(i = 0; i < count2 -1; i++){
                a[i] = g[i];
        }
        a[count2 - 1] = 1;
        return 0;
}

When going to gdb i notice that the value of

head->occursIn[0]

changes after the line

wordSize = strlen(trace->word);

and I have no clue why.

In your CreateNode() function, you are not allocating storage for the occursIn array. You are simply declaring a local array within the function and then assigning the occursIn pointer:

int a[1];
a[0] = 1;
new->occursIn = a;

The array a[1] goes away when the createNode function returns, so at that point your occursIn pointer is pointing to a value that is subject to being overwritten.

And even if the storage was allocated correctly in createNode, you've set a fixed size for the array but your whole strategy depends on that array having an element for each file; and in addOccurs you don't do anything to allocate a new larger array for a new file.

You may want to re-evaluate your strategy and switch to using lists instead of arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM