简体   繁体   English

释放内存时出现分段错误

[英]Segmentation fault error while freeing the memory

I tried to make a self-made program for lists.我试图为列表制作一个自制程序。 So, I created some basic stuff like creating the list, adding new nodes, showing them, and deleting all the existing nodes in the list.所以,我创建了一些基本的东西,比如创建列表、添加新节点、显示它们以及删除列表中的所有现有节点。

However, when I put in my list more than 27 elements, it throws me a segmentation fault error while freeing the memory.但是,当我在列表中放入超过 27 个元素时,它会在释放内存时引发分段错误。 By the way, when I add like 26 or smaller number of them, it works great.顺便说一句,当我添加 26 个或更少的数量时,效果很好。 Maybe stack is overflowed or something like that, I really have no idea.也许堆栈溢出或类似的东西,我真的不知道。

PS don't tell me that I`m developing a bike, in this way, making something by myself first, I understand things better:) PS不要告诉我我在开发自行车,这样,先自己做一些东西,我更了解事情:)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct node{
    struct node * next;
    int value;
} node;

int addNode(int position,int value, node ** head,int * size){
    int i = 2;
    node * curNode = *head;
    if(position > *size) return;
    while(i <= *size){
        if(position == 1){
            node * adress = *head;
            *head = (node*) malloc(sizeof(node*));
            (*head)->next = adress;
            (*head)->value = value;
            break;
        }

        else if(i == position){
            node * adress = curNode->next;
            curNode->next = (node*) malloc(sizeof(node*));
            curNode = curNode->next;
            curNode->next = adress;
            curNode->value = value;
            break;
        }
        else{
            curNode = curNode->next;            
            ++i;            
        }       
    }   
    ++(*size);      
    return;     
}

void showList(node * head, int size){
    int i; node * currentNode = head;
    for(i = 0; i < size; ++i){
        printf(" %d , next adress: %p |\n", currentNode->value, currentNode->next);
        currentNode = currentNode->next;
    }
    printf("\n");
}

void cleanList(node * head, int size){
    int i;
    node * curNode = head; node * nextToDelete = NULL;
    for(i = 0; i < size; ++i){
        nextToDelete = curNode->next;
        free(curNode);
        curNode = nextToDelete;
    }
}

int main() {
    node * head = (node*) malloc(sizeof(node*)); //saving head adress to know where the list starts
    head->value = 1; //set head value as "1"    
    int i, size;    
    node * currentNode = head; //pointer which points to a current node     
    for(i = 0; i < 5; ++i){         
        node * adress = (node*) malloc(sizeof(node*)); //variable which saves new node`s adress
        currentNode->next = adress; //set this new nod`s adress to previous node`s "next" parametr      
        currentNode = adress; //set new node`s adress to a current node 
        currentNode->value = i+2; ///set value for this node    
    }   
    size = 6;       
    addNode(2, 15, &head, &size);  
    showList(head, size);
    showList(head, size);  
    cleanList(head, size);
    return 0;
}

You are allocating memory incorrectly.您分配的内存不正确。

Notice these lines:注意这些行:

*head = (node*) malloc(sizeof(node*));

and

curNode->next = (node*) malloc(sizeof(node*));

You are allocating memory for a pointer to struct node instead of the actual struct.您正在为指向struct node而不是实际结构的指针分配内存。
Notice the sizeof function - you pass it the wrong parameter!注意sizeof函数——你传递了错误的参数!

Your structure contains an int and a pointer.您的结构包含一个int和一个指针。 Those are usually the same size.这些通常是相同的大小。
But you only allocate memory for a pointer, so, you allocate half the structure.但是你只为一个指针分配内存,所以,你分配了一半的结构。

This will cause you to call free on an invalid address at some point.这将导致您在某个时候free拨打无效地址。 It is a miracle your program only crashed during free operation, it should have crashed much sooner.你的程序只在free运行期间崩溃是一个奇迹,它应该早点崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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