Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie: Linked list

Reply
Thread Tools

Newbie: Linked list

 
 
irqbreaker@gmail.com
Guest
Posts: n/a
 
      02-11-2008
Hi,

Could someone please show me the obvious error I've made in the
following code example?

First, this example compiles and executes without fail on both Linux
(gcc 4.2.1), and
Solaris (GCC 3.4.6). But I get an Segmentation fail when I compile and
run on Windows
with djgpp (GCC 4.2.1).

gdb says:
Program received signal SIGSEGV, Segmentation fault.
0x0000204c in listDeleteNode (n=0x8e52c) at fail.c:56
56 (*n)->next->prev = (*n)->prev;

I thought that the test on line 54 made sure that I wasn't trying to
dereference a bad
pointer.

Anyway, here's the code:

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

typedef struct Node
{
int value;
char *name;
struct Node *prev;
struct Node *next;
} Node;

void *xMalloc(size_t s)
{
void *res = malloc(s);

if(!res && s) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}

return res;
}

Node *listAddNode(Node **n, int value, char *name)
{
Node *new = xMalloc(sizeof(Node));
char *newName = xMalloc(strlen(name)+1);

new->value = value;
strcpy(newName, name);
new->name = newName;

if(!*n) {
*n = new;
(*n)->next = NULL;
(*n)->prev = NULL;
} else {
new->prev = *n;
(*n)->next = new;
*n = new;
}

return new;
}

void listDeleteNode(Node **n)
{
Node *d = *n;

if(!d)
return;

if(d->prev && d->next) {
(*n)->prev->next = (*n)->next;
(*n)->next->prev = (*n)->prev;
*n = (*n)->next;
} else if(!d->prev && d->next) {
(*n)->next->prev = NULL;
*n = (*n)->next;
} else if(d->prev && !d->next) {
(*n)->prev->next = NULL;
*n = (*n)->prev;
}

free(d->name);
free(d);
}

void listFree(Node **n)
{
Node *tmp;

if(!*n)
return;

while(*n) {
tmp = *n;
*n = (*n)->next;
free(tmp->name);
free(tmp);
}
}

int main(void)
{
Node *head = NULL;
Node *tail = NULL;

head = listAddNode(&tail, 1, "Hello");
listAddNode(&tail, 2, "World");
listDeleteNode(&tail);
listAddNode(&tail, 3, "ICANHAZCHEEZBURGER");

listFree(&head);

return EXIT_SUCCESS;
}


Best regards,
Peter
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      02-11-2008

<(E-Mail Removed)> wrote in message
> Could someone please show me the obvious error I've made in the
> following code example?
>
> Node *listAddNode(Node **n, int value, char *name)
> {
> Node *new = xMalloc(sizeof(Node));
> char *newName = xMalloc(strlen(name)+1);
>
> new->value = value;
> strcpy(newName, name);
> new->name = newName;
>

Here you need new->next = NULL, new->prev = NULL;
Otherwise new->prev will dangle.
>
> if(!*n) {
> *n = new;
> (*n)->next = NULL;
> (*n)->prev = NULL;
> } else {
> new->prev = *n;
> (*n)->next = new;
> *n = new;
>
>
> }
>
> return new;
> }
>
>


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
 
 
 
irqbreaker@gmail.com
Guest
Posts: n/a
 
      02-11-2008
On 11 Feb, 15:17, "Malcolm McLean" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
> > Could someone please show me the obvious error I've made in the
> > following code example?

>
> > Node *listAddNode(Node **n, int value, char *name)
> > {
> > * *Node *new = xMalloc(sizeof(Node));
> > * *char *newName = xMalloc(strlen(name)+1);

>
> > * *new->value = value;
> > * *strcpy(newName, name);
> > * *new->name = newName;

>
> Here you need *new->next = NULL, new->prev = NULL;
> Otherwise new->prev will dangle.


Yes! Of course. Thank you very much for your help.

Best regards,
Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments