1

I have written a program on queues and dynamic memory allocation. This is what my program needs to do - insert values in to the queue and remove it from the queue; that simple. But my problem is that it just prints the names of the variables the values are assigned to and the program goes not responding.

Here is my program :

#include <stdio.h>
#define MAX 180

struct cakes{
        int spongecake;
        int meringue;
        int chocalate;
        int red_velvet;
        struct newcake *next;
};

struct Queue{
        int front;
        int rear;
        int count;
        int cake[10];
};

void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);

void cake_order(struct cakes *);
void order_out(struct cakes *);

main()
{
        struct cakes *head;

        head=(struct cakes *)malloc(sizeof(struct cakes ));
        cake_order(&head); //this is a seperate function and it works perfectly
        head->next=(struct cakes *)malloc(sizeof(struct cakes));
        order_out(&head->next);
}
void init(struct Queue *q)
{
        q->front=0;
        q->rear=10-1;
        q->count=0;
}

int isFull(struct Queue *q)
{
        if(q->count==10)
        {
                return 1;
        }
        else 
        {
                return 0;
        }
}

void insert(struct Queue *q,int x)
{
        if(!isFull(q))
        {
                q->rear=(q->rear+1)%10;
                q->cake[q->rear]=x;
                q->count++;
        }

}

int isEmpty(struct Queue *q)
{
        if(q->count==0)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

int removes(struct Queue *q)
{
        int caked=NULL;

        if(!isEmpty(q))
        {
                caked=q->cake[q->front];
                q->front=(q->front+1)%10;
                q->count--;
                return caked;
        }
}

void order_out(struct cakes *order)
{
        struct Queue s;
        int i;  

        order->spongecake=20;
        order->meringue=75;
        order->chocalate=40;
        order->red_velvet=30;

        init(&s);

        for(i=0;i<10;i++)
        {
                insert(&s,order->chocalate);
                insert(&s,order->spongecake);
                insert(&s,order->meringue);
                insert(&s,order->red_velvet);
    }

        while(!isEmpty(&s)) 
        {   
                printf("%d",removes(&s));
        }
}

What seems to be the problem here? I am new to C, so yea am a bit slow when debugging in this language.

Thank you for your time.

Here is the output:

enter image description here

Kara
  • 6,115
  • 16
  • 50
  • 57
Troller
  • 1,108
  • 8
  • 29
  • 47
  • Is `struct newcake` a `typedef`? – raj raj May 28 '13 at 04:58
  • its a pointer to a new structure – Troller May 28 '13 at 05:02
  • 2
    Then why is it like: `head->next=(struct cakes *)malloc(sizeof(struct cakes));` instead of `head->next=(struct newcake*)malloc(sizeof(struct newcake));`?? You meant `struct cakes *next` instead of `struct newcake *next`?? – raj raj May 28 '13 at 05:06
  • Is `newcake` meant to be different to `cake`? Because as raj raj has pointed out, maybe you did mean `struct cakes *next`. Unless there's another struct called `newcake` the definition of which is absent. – Nobilis May 28 '13 at 05:19
  • Also `cake_order(&head);` should actually be `cake_order(head);` as head is already a pointer (i.e. address in memory). It doesn't make any sense to be passing the address of the address in this case. For me this solved the segmentation fault problem. – Nobilis May 28 '13 at 05:23
  • Same goes for `order_out(&head->next);`, should be `order_out(head->next);` – Nobilis May 28 '13 at 05:33

1 Answers1

1

Lots of problems here, first it would be better if main was declared properly as in int main() and then it returned a value at the end e.g. return 0; like:

int main()
{
    .... // code

    return 0; // normally 0 is returned if execution has been successful
}

There seem to be other problems with the code as I wasn't able to compile it, for example there's no closing brace at the end of order_out() (right after the while loop).

Also would be good if you provided the cake_order() function.

It's also missing the includes for say stdlib.h, and on line 45 (head=(struct cakes *)malloc(sizeof(struct cakes ));) I've noticed you cast the result of malloc, which is not necessary.

And if I may further add, don't remember to free() the memory you've allocated with malloc(). I didn't see a single free() statement in your code.

Community
  • 1
  • 1
Nobilis
  • 7,310
  • 1
  • 33
  • 67
  • Well thanks for the reply , even after the edits, its still the same. – Troller May 28 '13 at 05:04
  • 1
    I've added a few other suggestions to the code, I would recommend you take them into account as at the very least it would make your program more predictable. I don't know if your compiler is spitting out any warnings but I'm certain it does. Better take those into account too, just because the code compiled doesn't mean it would work properly. – Nobilis May 28 '13 at 05:07