在Struct-C中释放String时的分段错误

时间:2013-01-27 23:18:15

标签: c malloc

我长期以来一直陷入分段错误。我声明了一个带有指向字符串的指针的结构。我写了两个函数,创建和删除来操纵值。结构如下:

#include "filename.h"  
//*in filename.h:* typedef struct linkNode linkNode_t;

struct linkNode{
    struct linkNode *next;
    char *value;
};

create function首先为节点分配内存,然后为该值分配内存,然后将输入值复制到value字段中:

linkNode_t* create(char* stuff){
    linkNode_t *ptr=malloc(sizeof(linkNode_t));
    if(ptr==NULL){
        printf("malloc failure");
        return NULL;
    }
    char* tempvalu=malloc(sizeof(char)*strlen(stuff)+1);
    if(tempvalu==NULL){
        printf("malloc failure");
        return NULL;
    }
    strcpy(tempvalu,stuff);
    ptr->next=NULL;
    ptr->value=tempvalu;
    return ptr;
}

一个函数用于将节点插入链表:

linkNode_t* insertLast(linkNode_t* start, linkNode_t* newNode){
    linkNode_t* current=start;
    while(current->next!=NULL){
        current=current->next;
    }
//now current points to the last element in the linked list
    current->next=newNode;
    return start;
}

导致我问题的部分如下:

linkNode_t* removebyValue(linkNode_t* start, char* valu){
/**removes the first instance of a node with a certain value. Return *start after     removing.
    if linked list becomes empty, return NULL*/

    linkNode_t *current=start;
    linkNode_t *previous=start;
    while(current!=NULL){
        if(strcmp(valu,current->value)==0) {//found the node to delete
            if(current==start){//removing the head
                linkNode_t* retvalue= current->next;
                free(current->value);
                free(current);
                return retvalue;
            }
            else{   //removing other elements in the linked list
                previous->next=current->next;
                free(current->value);
                free(current);
                return start;
            }
        }
        else{
            previous=current;
            current=current->next;
        }
    }
    return start;
}

在Main中,我创建了一个包含两个元素1和2的链表,并在发生分段错误时尝试释放元素1。

int main(){
    linkNode_t *pt1=create("1");
    pt1=insertLast(pt1,create("2"));
    removebyValue(pt1,"1"); //Causes seg fault. If I replace "1" by "2" nothing happens 

有人可以对此提出一些建议吗?提前致谢

编辑:我把所有可能相关的代码放在一起,因为有人说我放的部分没有错误

2 个答案:

答案 0 :(得分:2)

我认为您在正确维护启动指针的同时过度考虑删除节点。考虑一种希望更简单的方法。

typedef struct node_t 
{
    struct node_t* next;
    char* value;
} node_t;

node_t* remove(node_t *start, const char* valu)
{
    node_t* current=start;
    node_t* prev=NULL;

    while(current && strcmp(current->value, valu))
    {
        prev = current;
        current = current->next;
    }

    if (current)
    {
        if (prev) // we're not deleting start node
            prev->next = current->next;

        else      // we *are* deleting start node
            start = current->next;

        // now the node is unlinked. remove it.
        free(current->value);
        free(current);
    }
    return start;
}

答案 1 :(得分:1)

这是一个可行的替代测试代码,抓住它的战利品,看看它是否有帮助。 另外,你可以添加

typedef struct node_t {
    struct node_t* next;
    char* value;
} node;

这可能看起来更容易理解,但并不是因为typedef的性质令人困惑。 我强烈建议你看看https://github.com/torvalds/linux/blob/master/Documentation/CodingStyle 这是linux内核的编码风格,它非常简短,不是特别的法则,但值得注意......

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

struct node_t {
    struct node_t* next;
    char* value;
};

struct node_t* create(const char* istr) 
{
    struct node_t* ptr = (struct node_t*)malloc(sizeof(struct node_t));
    char* tmp = (char*)malloc(sizeof(char) * (strlen(istr) + 1));

    strcpy(tmp, istr);
    ptr->next = 0;
    ptr->value = tmp;
    return ptr;
}

struct node_t* remove(struct node_t* start, const char* value)
{
    struct node_t* current = start;
    struct node_t* prev = start;

    while (current != 0) {
        if (!strcmp(value, current->value)) {
            if (current == start) {
                struct node_t* retval = current->next;
                free(current->value);
                free(current);
                return retval;
            } else {
                /* nothing happens */
                return 0;
            }
        }
    }
}

int main(const int argc, const char** argv)
{
    struct node_t* pt = create("1");
    printf("%s\n", pt->value);
    pt->next = create("2");
    printf("%s\n", pt->next->value);
    remove(pt, "1");    

    return 0;
}
相关问题