C - Segmentation fault - insertion_sort链表函数

C - Segmentation fault - insertion_sort function of linked list

应用我在 Google (http://teknosrc.com/linked-list-in-c-insertion-sort/) 获得的一些排序方法后,我的函数插入不起作用。

首先,我将要使用的结构:

// 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;  

在这个函数中,nomeDaList是一个字符串(char *),用于其他函数查找列表,iItem

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;  
    }  
}    

请帮助解决此分段错误(核心已转储)。

你在这行打勾了
if(temp == NULL)
这应该并且通常会保护您免受通过 NULL 指针访问的段错误。
但是,在几行之前,您已经两次取消引用未选中的 temp
itemTemp = temp->item;

No * temp = auxLista->cabeca;

您应该更改代码以确保仅在 tmp 为非 NULL 时才执行这些行。例如。拆分变量定义及其初始化并将 init 移动到检查行之后。

您还从函数 criaNo(i) 接收到一个指针,并在几行之后使用它,而不检查它是否为 NULL。
ptr->prox=NULL;
目前尚不清楚,是否保证它是非 NULL。您将必须 "rubber-duck" 该函数,即详细检查它是否可以 return NULL。

这里很好地描述了如何在没有调试器的情况下进行调试(主要是),还解释了 "rubber-ducking"。 https://ericlippert.com/2014/03/05/how-to-debug-small-programs/

对于你不知道如何使用调试器的问题:
How to debug using gdb?

对于你不使用 IDE 的问题:
找到一个,省去痛苦。
我最喜欢的搜索引擎将我目前使用的免费 IDE 作为第一个匹配项(对于 "free IDE c"),我正在考虑切换到第三个匹配项。