Removing node from linked list 
Author Message
 Removing node from linked list

I have a problem with code I have written to step through a linked list
and remove nodes whose contents meet a certain condition. The code
compiles successfully, but my program exits when the free() function is
called (inside my ObjRemove() function). If I comment out the call to
free everything works, but of course then I'm not freeing the memory.
When I initialise the list I create a dummy node, so when Node->next ==
NULL I have actually reached the end of the valid data in the list.
The essentials of the code look like so: (calling code at the bottom)

typedef struct ObjListNode {
                           OBJECT Object;
                           ObjListNode* next;
                           ObjListNode* prev;
                           } OBJECT_LISTNODE;

 /************************************************
 * FUNCTION:  ObjRemove                          *
 *                                               *
 * Removes an node from a linked list of         *
 * object structures, returns pointer to the     *
 * next node                                     *
 *                                               *
 ************************************************/
 OBJECT_LISTNODE *ObjRemove(OBJECT_LISTNODE *Node)
   {
   OBJECT_LISTNODE *NextNode = Node->next;
   if(Node->prev == NULL)          
      // node is at start of list      
      {
      Node->next->prev = NULL;
      }
   else
      // node is anywhere else in list                            
      {
      Node->prev->next = Node->next;
      Node->next->prev = Node->prev;
      }
   free(Node);
   return NextNode;
   }        

// CALLED FROM HERE
Node = /* the head of some linked list */
while(Node->next != NULL)
   {
   if( /* some condition is met */ )
      {
      Node = ObjRemove(Node);
      }
   else
      {
      Node = Node->next;
      }
   }

Any advice would be greatly appreciated

--
Don Halloran



Fri, 25 Oct 2002 03:00:00 GMT  
 Removing node from linked list

Quote:

> I have a problem with code I have written to step through a linked
list
> and remove nodes whose contents meet a certain condition. The code
> compiles successfully, but my program exits when the free() function
is
> called (inside my ObjRemove() function). If I comment out the call to
> free everything works, but of course then I'm not freeing the memory.
> When I initialise the list I create a dummy node, so when Node->next
==
> NULL I have actually reached the end of the valid data in the list.
> The essentials of the code look like so: (calling code at the bottom)

I've looked at your code several times and it looks right to me.
I either have the same blind spot or the problem is elsewhere.

What do you mean by "my program exits when the free() function is
called"?  If it crashes or never returns from free (e.g., it doesn't
execute a fprintf(stderr,"after free\n") immediately following the
free()), then you probably corrupted the dynamic allocation mechanism
by writing outside the object you allocated or by giving free a
pointer that wasn't returned by malloc (or related function).

Did you malloc the correct size of node?  Are you trying to free
a node that was not allocated by free?

Are you sure that the linked list is set up correctly before you
remove elements?  And that it's still OK afterward, with no free?
Write a test program that explicitly allocates and initializes
a short linked list, and write a debug routine that prints out
each allocated node (using %p for pointers); delete nodes (without
free) and call the debug routine after each node deletion.

--
MJSR

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 25 Oct 2002 03:00:00 GMT  
 Removing node from linked list
On Mon, 08 May 2000 23:26:14 +1000, Donald Halloran
...

Quote:
>typedef struct ObjListNode {
>                           OBJECT Object;
>                           ObjListNode* next;
>                           ObjListNode* prev;
>                           } OBJECT_LISTNODE;

The above declaration is not valid C code.  If your compiler accepts it,
it's being provided as an extension.  You may be using a C++ compiler
instead of a C compiler.  In C, you must declare next and prev as
     struct ObjListNode *next;
     struct ObjListNode *prev;
because a structure tag is not a type name.

...
[rest of code snipped]

Except for the fact that you're not freeing the last node in the list, the
code looks okay to me.  I was about to write that the code fails when
Node->next == NULL, but then I saw that you don't remove that last node.
I suspect that whatever is causing the failure is somewhere else in the
program, in code you haven't shown us.  I suggest stepping through this part
of the code with a de{*filter*}, if you have one available.  Barring that, try
to post a minimal complete program that displays the problem.

--

Kenan Systems Corp., a wholly-owned subsidiary of Lucent Technologies



Fri, 25 Oct 2002 03:00:00 GMT  
 Removing node from linked list
Hi,
Your problem is that you call free on Node as stated in your function
declaration:
OBJECT_LISTNODE *ObjRemove(OBJECT_LISTNODE *Node)
Node is a pointer to an OBJECT_LISTNODE. You cannot free a pointer to an
OBJECT_LISTNODE unless you malloc one. What you need to do is call free on
the struct that Node points to. In other words free(*Node);
That should fix your problem.

Stephen

Quote:

> I have a problem with code I have written to step through a linked list
> and remove nodes whose contents meet a certain condition. The code
> compiles successfully, but my program exits when the free() function is
> called (inside my ObjRemove() function). If I comment out the call to
> free everything works, but of course then I'm not freeing the memory.
> When I initialise the list I create a dummy node, so when Node->next ==
> NULL I have actually reached the end of the valid data in the list.
> The essentials of the code look like so: (calling code at the bottom)

> typedef struct ObjListNode {
>                            OBJECT Object;
>                            ObjListNode* next;
>                            ObjListNode* prev;
>                            } OBJECT_LISTNODE;

>  /************************************************
>  * FUNCTION:  ObjRemove                          *
>  *                                               *
>  * Removes an node from a linked list of         *
>  * object structures, returns pointer to the     *
>  * next node                                     *
>  *                                               *
>  ************************************************/
>  OBJECT_LISTNODE *ObjRemove(OBJECT_LISTNODE *Node)
>    {
>    OBJECT_LISTNODE *NextNode = Node->next;
>    if(Node->prev == NULL)
>       // node is at start of list
>       {
>       Node->next->prev = NULL;
>       }
>    else
>       // node is anywhere else in list
>       {
>       Node->prev->next = Node->next;
>       Node->next->prev = Node->prev;
>       }
>    free(Node);
>    return NextNode;
>    }

> // CALLED FROM HERE
> Node = /* the head of some linked list */
> while(Node->next != NULL)
>    {
>    if( /* some condition is met */ )
>       {
>       Node = ObjRemove(Node);
>       }
>    else
>       {
>       Node = Node->next;
>       }
>    }

> Any advice would be greatly appreciated

> --
> Don Halloran



Fri, 25 Oct 2002 03:00:00 GMT  
 Removing node from linked list

Quote:


> > I've looked at your code several times and it looks right to me.
> > I either have the same blind spot or the problem is elsewhere.

> I got it I got it! Woohoo.

[snip details - delete of first node left dangling pointer]

Hmm.  Yes, I should have noticed that.  Probably didn't because
of my lamentable failure to use square roots in my programs.

--
MJSR

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 25 Oct 2002 03:00:00 GMT  
 Removing node from linked list

Quote:

> I've looked at your code several times and it looks right to me.
> I either have the same blind spot or the problem is elsewhere.

I got it I got it! Woohoo.
The problem was in this part of my code:

   if(Node->prev == NULL)          
      // node is at start of list      
      {
      Node->next->prev = NULL;
      }

The problem here is that when I free(Node), the pointer to the start of
the list is now pointing to memory that I have freed. I solved it by
changing the ObjRemove function to:

 OBJECT_LISTNODE *ObjRemove(OBJECT_LISTNODE *Node,
                            OBJECT_LISTNODE **ListHead)
   {
   OBJECT_LISTNODE *NextNode = Node->next;
   if(Node->prev == NULL)          
      // case where node is at start of list    
      {
      Node->next->prev = NULL;
      *ListHead = Node->next;
      }
   else
      // case where node is anywhere else in list                    
      {
      Node->prev->next = Node->next;
      Node->next->prev = Node->prev;
      }
   free(Node);
   return NextNode;
   }        

So now if I am removing the first entry in the list, I move the pointer
to the start of the list forward one.
It took me so long to figure out what was going wrong here, not helped
by the fact that this is actually part of a directX application and
therefore stupidly difficult to debug. I feel SOOOOOOO much better now!!
:))

--
Don Halloran



Sat, 26 Oct 2002 03:00:00 GMT  
 Removing node from linked list

Quote:

> On Mon, 08 May 2000 23:26:14 +1000, Donald Halloran

> ...
> >typedef struct ObjListNode {
> >                           OBJECT Object;
> >                           ObjListNode* next;
> >                           ObjListNode* prev;
> >                           } OBJECT_LISTNODE;

> The above declaration is not valid C code.  If your compiler accepts it,
> it's being provided as an extension.  You may be using a C++ compiler
> instead of a C compiler.  In C, you must declare next and prev as
>      struct ObjListNode *next;
>      struct ObjListNode *prev;
> because a structure tag is not a type name.

Ah, good point. Yes I am in fact using a C++ compiler, but I will change
it to correct C syntax anyway, because I don't officially know C++ yet.
Thanks.

--
Don Halloran



Sat, 26 Oct 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Removing a NODE for good (double linked lists)

2. nodes linked listed and such

3. 2 nodes end up in a cycle in a single linked list

4. newbie question: removal of node in linked list

5. How to find a node in link list

6. Realloc()ing data of a node from a linked list

7. why malloc for a new node in linked list

8. Destroying All Nodes in a Linked List

9. Destroying All Nodes is a Linked List

10. deleting a previous node in a singly linked list

11. Question about swapping nodes in a doubly linked list

12. Deleting a node in a linked list

 

 
Powered by phpBB® Forum Software