PedroCunha_67677
PedroCunha_67677

Reputation: 47

Segmentation Fault(core dumped) - Structure;

So I have the following question:

I have this struct ListAut

struct ListAut{
    char* biggestn;
    int sizeof_biggestn;
    int total_len;
    struct node* avl;
};

Its typedef is as it follows:

typedef struct ListAut *IndexOfAuts;

IndexOfAuts *newIndexOfAuts()
{
    int i;
    IndexOfAuts *ioa = malloc(27 * sizeof(struct ListAut));

    for (i = 0; i < 27; i++)
    {
        ioa[i]->biggestn = "";
        ioa[i]->sizeof_biggestn = 0;
        ioa[i]->total_len = 0;
        ioa[i]->avl = NULL;
    }
    return ioa;
}

void insertName(IndexOfAuts * ioa, char *nome)
{
    char *aux = malloc(sizeof(nome));

    aux = trim(nome);
    int index = getIndexOfLetter(aux);

    if (nameLen(aux) > getSizeOfLongName(ioa[index]))
    {
        strcpy(ioa[index]->biggestn, aux);
        ioa[index]->sizeof_biggestn = nameLen(aux);
    }
    ioa[index]->total_len += nameLen(aux);
    insert(ioa[index]->avl, aux);
}

This is an important part of a module I need for a project, and on its main it's Seg Faulting. I suspect it's on the creation of an "object" newIndexOfAuts(), The idea of this module is to have an array of 27 pointers to those structures, one to each letter and another to the special characters; Now I'm just confused because it might be from the problem above or from a module loader I made:

void loadModules(char *filename, IndexOfAuts * ioa, StatTable st)
{
    char *nameofile = malloc(20);

    strcpy(nameofile, filename);
    FILE *file = fopen(nameofile, "r");

    if (file != NULL)
    {
        int counter, ano;
        char *buff, *field, *auxil;

        buff = malloc(1024);
        field = malloc(200);
        auxil = malloc(200);
        while (fgets(buff, 1024, file))
        {
            counter = 0;
            field = strtok(buff, ",");
            printf("inserting 1st name\n");
            insertName(ioa, field);
            counter++;
            while (!atoi(field))
            {

                if ((auxil = strtok(NULL, ",")) != NULL)
                {
                    counter++;
                    field = auxil;
                    insertName(ioa, field);

                }
            }
            ano = atoi(field);
            incPub(st, ano, counter - 1);
        }
        fclose(file);

    }
}

When i run this in main that has the following lines:

printf("Creating Stat Table");
StatTable st=newStatTable();\\This Line is correct, i checked it,i hope
printf("Creating index");
IndexOfAuts* ioa=newIndexOfAuts();
printf("Loading Modules");
loadModules(filename,ioa,st);

Those prints were for me to see where was the cause of the seg fault, but the last line printed was the "Creating Index".

Upvotes: 0

Views: 271

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

There are several cases of undefined behavior and one memory leak (and a possible case of undefined behavior too):

  1. You have this initialization ioa[i]->biggestn=""; It make the biggestn member point to a constant array of one character (the '\0' character). You then do strcpy(ioa[index]->biggestn,aux); which will write over a constant character, and then go outside of the array writing into unknown memory.

  2. You have this: char* aux=malloc(sizeof(nome)); That allocates only 4 or 8 bytes, which the size of the pointer and not what the pointer points to. Use strlen to get the length of a string.

  3. For the above allocation you also need to allocate a byte extra, as strlen only returns the length of the string without the terminator.

  4. You have aux=trim(nome); This overwrites the pointer you just allocated, leading to a memory leak.

  5. The above call might also lead to undefined behavior if you return a pointer to a local variable or array.

There are probably other problematic lines, these were just the ones I found on a quick glance.


And a general tip: Learn to use a debugger! The debugger is a programmers best tool next to the compiler. If you run your program in a debugger, the debugger will stop at the location of the crash, and let you examine (and also walk up) the function call stack, as well as let you examine values of variables.

Upvotes: 6

Related Questions