0

I'm trying to pass a pointer to a queue into the createQueue function:

void createQueue(struct pqueue *queue){
    queue = malloc( sizeof(struct pqueue) );  
    queue->root = malloc(sizeof(struct node));
    queue->root->next = 0;   
    queue->root->taskID = 12;
    queue->root->priority = 5000;
}

I also try to add to the newly created queue like this:

void add(struct pqueue *queue, int taskID, int priority){
struct node *conductor;
conductor = queue->root;
if ( conductor != 0 ) {
        while ( conductor->next != 0)
        {
                conductor = conductor->next;
        }
}
 conductor->next = malloc( sizeof(struct node) );  
 conductor = conductor->next;
 if ( conductor == 0 )
  {
      printf( "Out of memory" );
  }
  /* initialize the new memory */
  conductor->next = 0;         
  conductor->taskID = taskID;
  conductor->priority = priority;
}

from the main function:

int main()
{
    struct pqueue *queue;       

    createQueue(queue);
    add(queue, 234093, 9332);
}

...but I keep segfaulting. Any reason why this keeps happening?

EDIT:

The structs for pqueue and node are like this:

struct node {
  int taskID;
  int priority;
  struct node *next;
};

struct pqueue{
  struct node *root;
};
idungotnosn
  • 2,001
  • 4
  • 29
  • 36
  • You [Need of Pointer to pointer](http://stackoverflow.com/questions/18306935/need-of-pointer-to-pointer/18307020#18307020) – Grijesh Chauhan Oct 09 '13 at 19:21
  • What do `pqueue` and `node` look like? – Brendan Long Oct 09 '13 at 19:21
  • 1
    `queue = malloc( sizeof(struct pqueue) ); ` You are assigning to a local variable. The calle wont see. Put the malloc in the caller and you'll be Ok. Also you could make the created queue the **return value** of the function, instead of the useless `void()` – wildplasser Oct 09 '13 at 19:21
  • 1
    Is that all the code required to seg fault or are you seg faulting after `createQueue` when you attempt to use `queue`? – Shafik Yaghmour Oct 09 '13 at 19:22
  • Literally the same question was asked yesterday with a different function name and struct tag. –  Oct 09 '13 at 19:24
  • @ShafikYaghmour Investigating right now. –  Oct 09 '13 at 19:26
  • @ShafikYaghmour how do I pass in the queue to the createQueue function then? – idungotnosn Oct 09 '13 at 19:30
  • 1
    See @GrijeshChauhan comment at the top or the answer below, please in the future always provide an SSCCE, it will prevent us guessing what the problem really is. – Shafik Yaghmour Oct 09 '13 at 19:30

1 Answers1

4

In C, everything is passed by value. Therefore, when you call createQueue(queue), you are passing a copy of the pointer to the function. Then, inside the function, when you say queue = malloc(...), you are setting that copy of the pointer equal to your newly allocated memory - leaving main()'s copy of that pointer unchanged.

You want to do something like this:

void createQueue(struct pqueue **queue)
{
    (*queue) = malloc( ... );
}

int main(void)
{
    struct pqueue *queue;

    createQueue(&queue);
}

This question has a more detailed description of what's going wrong for you.

Community
  • 1
  • 1
CmdrMoozy
  • 3,870
  • 3
  • 19
  • 31