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; } } 
6
  • 2
    Is p passed in to the allocator function as an argument (of type node *) by any chance? Commented Feb 10, 2012 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. Commented Feb 10, 2012 at 11:24
  • Yes p is declared as node *p; Commented Feb 10, 2012 at 11:28
  • The standard header where the prototype for malloc() is, is <stdlib.h>. You don't need "alloc.h" (whatever that is) ... Commented Feb 10, 2012 at 11:35
  • I have included stdlib.h Commented Feb 10, 2012 at 11:36

2 Answers 2

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; } } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I realize now . Thanks hmjd . I ll try this.
The comment about malloc in this answer is important, read this.
hmjd , You were absolutely right , Yes I had failed to change the pointer to the queue head . Thanks very much.
Thanks I didnt know . :D Ok !
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); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.