Reputation: 53
I am trying to learn data structures and I am struggling with getting this code to work. Problem is I am getting segmentation fault(core dumped)
with gcc C compiler. It is supposed to be a queue. The code:
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 50
struct node{
int data;
struct node * next;
};
struct queue{
int count;
struct node * rear ,* front;
};
void create (struct queue * q) {
q -> front = NULL;
q -> rear = NULL;
q -> count = 0;
}
int isempty(struct queue * q) {
if(q -> count == 0)
return 1;
return 0;
}
int full(struct queue * q) {
if(q -> count == STACK_SIZE) {
return 1;
}
return 0;
}
void enqueue(struct queue * q , int x) {
struct node * temp;
temp = (struct node*) malloc(sizeof(struct node));
temp -> data = x;
temp -> next = NULL;
if (full(q)) {
printf("Not possible. Overflow.");
}
else if(isempty(q)) {
q -> front = q -> rear = temp;
} else {
q -> rear -> next = temp;
q -> rear = temp;
}
q -> count++;
}
int dequeue (struct queue * q) {
struct node * p;
p = (struct node*) malloc (sizeof(struct node));
p = q -> front;
if(isempty(q)) {
printf("Not possible. Underflow.");
} else {
q -> front = q -> front -> next;
q -> count--;
}
int x = (p -> data);
return x;
}
int main (void) {
struct queue *q;
create (q);
enqueue(q, 5);
}
The problem is most probably usage of pointers. I've reviewed it a few times but no solution. Valgrind and gdb debuggers weren't much of a help, either.
Upvotes: 0
Views: 2592
Reputation: 11377
As mentioned by OctaveL, in the function create you try to set the fields of the queue but the pointer passed to the function does not point to a queue, it is uninitialized. If you add the option -Wall
to gcc it will actually warn you about this:
$ gcc -o test -Wall test.c
test.c: In function 'main':
test.c:71:5: warning: 'q' is used uninitialized in this function [-Wuninitialized]
create (q);
^~~~~~~~~~
Solution 1: Declare q as a record and pass the address of q to the function create:
struct queue q;
create (&q);
Solution 2: Declare q as a pointer and allocate a new queue variable:
struct queue *q;
q = malloc(sizeof *q);
create(q);
I would also advice you to rename the function create to init or clear since it doesn't create a new queue, it only initializes (or clears) it.
To make memory allocation easier and to handle errors properly it is convenient to introduce two macros:
#define NEW_ARRAY(ptr, n) \
(ptr) = malloc((n) * sizeof (ptr)[0]); \
if ((ptr) == NULL) { \
fprintf(stderr, "Memory allocation failed: %s\n", strerror(errno)); \
exit(EXIT_FAILURE); \
}
#define NEW(ptr) NEW_ARRAY(ptr, 1)
With these in place and if create is renamed to init you can write solution 2 as
struct queue *q;
NEW(q);
init(q);
Upvotes: 3
Reputation: 1045
You didn't allocate memory for q
in your main()
, so it crashes when attempting to access q->front
in create()
.
int main (void) {
struct queue *q; // No allocation here
...
}
You probably wanted this, which works just fine:
int main (void) {
struct queue q;
create (&q);
enqueue(&q, 5);
}
Upvotes: 2