1

The code is as follows , Seems like nothing wrong with it at all. My gcc doesnt find alloc.h

print(node *q)
    39  {   
    40      node *p=q;
    41              do
    42      {
    43          printf("%d",p->n);
    44          if(p->ptr != NULL)
    45          p=p->ptr;   
    46          else

(gdb) p p $1 = (node *) 0x0

And the code where memory is allocated is

    if(p== NULL)
    {
            p=(node *)malloc(sizeof(node));
            if(p== NULL)
                    printf("The malloc failed\n");
            p->n=num;
            p->ptr=NULL;
    }

When I run this in debugger there is no message of malloc failed.

Can anyone help. Regards

Sraddha

    add(node **q)
    {
         node *p=*q;
         int num;
         printf("Enter the number you want to add");
         scanf("%d", &num);
         if(p== NULL)
         {
            p=(node *)malloc(sizeof(node));
            if(p== NULL)
                    printf("The malloc failed\n");
            p->n=num;
            p->ptr=NULL;
         }
    }
sraddhaj
  • 581
  • 1
  • 7
  • 17
  • 2
    Is `p` passed in to the allocator function as an argument (of type `node *`) by any chance? – Mat Feb 10 '12 at 11:24
  • Could you edit your post to show the rest of that loop please, and also say where the segv occurs? And, as Mat suggests, also show the whole allocation function. – Useless Feb 10 '12 at 11:24
  • Yes p is declared as node *p; – sraddhaj Feb 10 '12 at 11:28
  • The standard header where the prototype for `malloc()` is, is ``. You don't need "`alloc.h`" (whatever that is) ... – pmg Feb 10 '12 at 11:35
  • I have included stdlib.h – sraddhaj Feb 10 '12 at 11:36
  • lIke the gdb output I show in the question.gdb) p p $1 = (node *) 0x0 The segmentation fault occurs at (gdb) run Starting program: /home/vaishali/linklist Enter 'n' when u want to stop7 Program received signal SIGSEGV, Segmentation fault. 0x08048583 in print (q=0x0) at linklist.c:42 – sraddhaj Feb 10 '12 at 11:37

2 Answers2

5

You need to assign to *q in the add() function, not the local p:

add(node **q)
{
     int num;
     printf("Enter the number you want to add");
     scanf("%d", &num);
     if(*q == NULL)
     {
        *q = malloc(sizeof(node)); /* no need to cast return value. */

        /* Corrected if logic to not access failed malloc. */
        if(*q == NULL)
        {
            printf("The malloc failed\n");
        }
        else
        {
            *q->n=num;
            *q->ptr=NULL;
        }
     }
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
0

In addition to the issues addressed by hmjd's answer, you might want to consider a different program design. You have made function does three entirely different things: interaction with the user, allocation of memory and the actual algorithm. This program design was the actual culprit that caused the bug.

Instead, you might want to consider a more object-oriented approach:

int prompt_user (void) // this function interacts with the user ("GUI")
{
  int num;
  printf("Enter the number you want to add");
  scanf("%d", &num);
  getchar(); // discard line feed character from stdin

  return num;
}

void init_node (node* new_node, int num)
{
  new_node->n = num;
  new_node->ptr = NULL;
}


// this code is the caller:
{
  node* q = NULL;

  ...

  int num = prompt_user();

  if(q == NULL)
  {
    q = malloc(sizeof(node));
    if(q == NULL)
    {
      // error handling
    }
  }  

  init_node(q, num);

  ...

  free(q);
}
Lundin
  • 195,001
  • 40
  • 254
  • 396