Is array C being overwritten / deleted? very confused

I am working on a project in C and it works great, except for one function that seems to rewrite my array and write strange numbers like 1970802352 which contains the number of occurrences of words in the 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 rewrites 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 rest of the functions:

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

head-> occursIn [0]

changes after the line

wordSize = strlen (trace-> word);

and I do not know why.

+3


source to share


1 answer


In your CreateNode () function, you are not allocating memory for the incomingIn array. You just declare a local array inside the function and then assign the incomingIn pointer:

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

      

Array a [1] disappears when createNode returns, so at this point yourInIn pointer points to a value that can be overwritten.



And even if the storage was properly allocated in createNode, you set a fixed size for the array, but your whole strategy depends on this array, having an element for each file; and in addOccurs you do nothing to allocate a new larger array for a new file.

You can reevaluate your strategy and switch to using lists instead of arrays.

+2


source







All Articles