【问题标题】:C - Linked List Concatenation Runs into malloc Error [closed]C - 链表连接遇到 malloc 错误 [关闭]
【发布时间】:2016-02-05 23:56:29
【问题描述】:

我在创建 concat(); 函数时遇到问题,它可以工作,但是当我继续释放列表时,我收到 malloc 错误...我很肯定我正在正确释放我的列表,当我不要运行我的concat(); 函数,列表会被正确释放。这是我相信我在这个函数中做错了什么的理由......

typedef struct node {
    ElemType val;
    struct node *next;
} NODE;


struct list_struct {
    NODE *front;
    NODE *back;
};


   void lst_concat(LIST *a, LIST *b) {
if( !( a->front && b->front)) return ;

if (a->front == NULL && b->back == NULL) {

    return;
}

if (a->front == NULL && b->front != NULL) {
    a->front = b->front;
    return;
}
else{
NODE *tmp = a->front;
    if( a->back == NULL )return;
    if( a->front == NULL )return;
    if( b->back == NULL )return;
    if( b->front == NULL )return;
    else{
    NODE *tempPtr = a->back;

    tempPtr->next=b->front;
    tempPtr = b->front;
    a->back = b->back;
    }
}}
void lst_free(LIST *l) {
NODE *p = l->front;
NODE *pnext;

  while(p!= NULL) {
    pnext = p->next;   // keeps us from de-referencing a freed ptr
    free(p);
    p = pnext;
  }
  // now free the LIST 
  free(l);
}

**UPDATE,此更新列表正确,但是,我仍然可以释放两个列表,还是我可以只释放一个,因为它们已加入?我的lst_free() 在上面。

【问题讨论】:

  • a->back = a->back;看起来不对
  • @bruceg 我很抱歉,我不知道它是如何到达那里的......它假设是 'b' 它已修复,但该功能仍然无法正常工作..
  • @milevyo 我不确定你在问什么,抱歉!:(
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable Example。
  • 这里有几个错误。我认为您的问题更为根本 - 显示所有代码...例如LIST....

标签: c linked-list concatenation concat


【解决方案1】:

这是我能想象的唯一合理的方式(到目前为止)......

typedef struct NODE         NODE;
typedef struct list_struct  LIST;


typedef struct node {
    ElemType    val;
    NODE        *next;
} NODE;


struct list_struct {
    NODE        *front;
    NODE        *back;
};


// get the ending node
NODE *LastNode(NODE *root){
    NODE *tmp = NULL;
    while(root){
        tmp  = root;
        root = root->next;
   }
   return tmp;
}


// this will move node from pnode
// and put it at the end of proot nodes
void move_node(NODE **proot,NODE **pnode){

    if(! (pnode && proot) )
        return; // no comments

    if(pnode == proot)
        return; // no comments

    NODE *node = *pnode;
    NODE *root = *proot;
    NODE *tmp;

    if(!node) return;

    if(!root){
        // this node becomes the first one
        *proot      = node;
    }else{
        // this node becomes the last node
        LastNode(root)->next = node;
    }

    // now nodes blongs to other node
    // set this to NULL
    *pnode = NULL;

}


// this will move a nodes to b;
// 
void lst_concat(LIST *a, LIST *b) {
    move_node(&a->front, &b->front);
    move_node(&a->back, &b->back);
    // at this point b has no front, and no back
    // all has been moved to a

}


void free_nodes(NODE **proot){
    if ( !(proot && *proot) )
        return;

    NODE *root = *proot;

    if( root->next)
        free_nodes(&root->next);

    free(root);
    *proot=NULL;
}

void lst_free(LIST *l) {
    if( l ) {
        free_nodes( &l->front);
        free_nodes( &l->back);
        free(l);
    }
}

【讨论】:

  • 在那些不投票的 Ninjaaaas 旁边。 (无论如何它们都很酷)。 @LearningCODE 你能解释一下你打算如何解决这种情况吗???,我该怎么办?
  • 我不知道,但也许你的介绍 I couldn't get a logical solution 很糟糕 - 要么你知道并发布一个好的解决方案,要么你不知道并且不发布。
  • @4386427,你认为我如何将整个代码放在评论中?我想让他明白问题出在哪里。
  • @milevyo 我的程序可以运行,但是当列表被释放时,我的 lst_free 函数中出现了 malloc 错误 pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
  • 我没有使用此代码,但是,您提醒我最后要使 LIST *b = NULL ... 这是我的问题,谢谢您的间接帮助:D
【解决方案2】:

在知道它是否是 null 之前,您正在取消引用 a 这里:

void concat(LIST *a, LIST *b)
{
    if (a->front == NULL) { // !!!! what if a is NULL? Crash...
        a->front = b->front; 
        a->back = b->back;
        return;
    }

检查a 是否为NULLfirst。

在知道它是否是 null 之前,您还取消了对 b 的引用:

void concat(LIST *a, LIST *b)
{
    if (a->front == NULL) {
        a->front = b->front; // !!!! what if b is NULL? Crash...
        a->back = b->back;
        return;
    }

至少这样做:

void concat(LIST *a, LIST *b)
{
    if (a == NULL)  // This needs to go first, i.e. before dereferencing a
    {
         // some code
    }

    // other code ....
}

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多