1

Why does the program crash? Although sometimes it doesn't. I'm using visual studio 2017. I've run my entire program (more functions) with an online compiler and it doesn't crash. Thank you. [Image of the crash21 [Image of the crash]2

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
}node;

typedef struct {
    int count;
    node *front;
    node *rear;
}QueueHead;

QueueHead *CreateQueue(){
    QueueHead *myQH = (QueueHead*)malloc(sizeof(myQH));
    //Empty list is created.
    //Emelent counter is set to zero
    myQH->count = 0;
    //Top should address NULL as it is an empty list
    myQH->front = NULL;
    myQH->rear = NULL;

    return myQH;
}

int main() {
    QueueHead *myQH = CreateQueue();
}
Nick
  • 57
  • 6

1 Answers1

0

Try changing the line of code

QueueHead *myQH = (QueueHead*)malloc(sizeof(myQH));

to

QueueHead *myQH = (QueueHead*)malloc(sizeof(QueueHead));
wookiekim
  • 1,156
  • 7
  • 20