-1

My function insert is not working, after applying some sorting methods that I got at Google (http://teknosrc.com/linked-list-in-c-insertion-sort/).

Firstly, the structures that I'm going to use at it:

// This is the Node , where will be stored the item  

struct no  
{  
        Item * item;  
        struct no *prox;  
};  
typedef struct no No;  

//This is the list, where will have the head node(No)  

struct lista  
{  
        char *nomeLista; //This is just a name of the list  
        No *cabeca; //This is the head node  
        int tamanho; //This is the amount of items inserted (forgot to implement this)  
        struct lista *prox; //This is the next list  
};  
typedef struct lista Lista;  

//This is just the main list that will guard all the list of nodes in a linked way. No need to worry about this.  

struct vetorListas  
{  
        Lista *cabeca; //head list  
        int tamanho; //amount of lists  
};  
typedef struct vetorListas VetorListas;  

//This is the item to be inserted  

struct item  
{  
        int id; //the ID used for the comparison of sort  
        char *nome; //just the name of it  
};  
typedef struct item Item;  

In this function, nomeDaList is a string (char *) used to find the list by other function and i is the Item:

void *  
insert(void * nomeDaLista, Item * i)  
{  
    Lista * auxLista; //the list
    auxLista = idl(nomeDaLista); //the function to get the list by it's name. It works, no worries.  


    //down here, is the sequence of codes translated to my program (got by the website I showed before)  

    No * temp = auxLista->cabeca;  
    No * prev = NULL;  
    No * ptr;  

    Item * itemTemp;  
    itemTemp = temp->item;  

    ptr = criaNo(i); //this function creates (makes the malloc and all) a node (No) and return the created node.



    if(temp == NULL)  
    {  
        ptr->prox=NULL;  
        auxLista->cabeca = ptr;  
        return auxLista;  
    }  

    if(i->id < itemTemp->id)  
    {  
        ptr->prox = auxLista->cabeca;  
        auxLista->cabeca = ptr;  
        return auxLista;  
    } else  
    {  
        while(temp != NULL)  
        {  
            if(i->id > itemTemp->id)  
            {  
                prev = temp;  
                temp = temp->prox;  
                continue;  
            } else  
            {  
                prev->prox = ptr;  
                ptr->prox = temp;  
                return auxLista;  
            }  
        }  

        prev->prox = ptr;  
    }  
}    

Please help with this Segmentation fault (core dumped).

arghtype
  • 4,376
  • 11
  • 45
  • 60

1 Answers1

0

You have a check in this line
if(temp == NULL)
which should, and normally would, protect you against segfaults from accessing via NULL pointer.
However, a few lines before, you already dereference the unchecked temp, twice.
itemTemp = temp->item;
and
No * temp = auxLista->cabeca;

You should change the code to make sure that these lines only get executed, if tmp is non-NULL. E.g. split the variable definition and its initialisation and move the init after the check line.

You also receive a pointer from a function criaNo(i) and use it a few lines later, without checking it against NULL.
ptr->prox=NULL;
It is not clear, whether that is guaranteed to be non-NULL. You will have to "rubber-duck" that function, i.e. check in detail, whether it can return NULL.

Here is a nice decription of how to debug (mostly) without a debugger, also explaining "rubber-ducking". https://ericlippert.com/2014/03/05/how-to-debug-small-programs/

For your problem of not knowing how to use a debugger:
How to debug using gdb?

For your problem of not using an IDE:
Find one, save pain.
My favorite search engine gives (for "free IDE c") my currently used free IDE as first match, the one I am thinking of switching to as third.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54