段错误(核心已转储)malloc 分配出错,知道的请进来
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define TRUE 1
#define FALSE 0
typedef struct TNode {
char data;
struct TNode * LChild;
struct TNode * RChild;
}BiTNode , *BiTree;
typedef struct QNode {
BiTree data;
struct QNode * next;
}LinkQueueNode;
typedef struct {
LinkQueueNode * front;
LinkQueueNode * rear;
}LinkQueue;
int InitQueue(LinkQueue * Q)
{
Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));//fail
if(Q->front!=NULL)
{
Q->rear = Q->front;
Q->front->next=NULL;
return (TRUE);
}
else return (FALSE);
}
int main()
{
LinkQueue* Q;
InitQueue(Q);
printf("ok\n");
return 0;
}
~
~
为什么分配失败,空间肯定足够。。。如何解决?错在哪? malloc C
[解决办法]
int main()
{
LinkQueue* Q = (LinkQueue *) malloc(sizeof(LinkQueue)); // Q 未初始化
if(Q) {
InitQueue(Q);
}
printf("ok\n");
return 0;
}