请教:调用malloc时出现Access violation writing location的错误怎么办??
#include"stdio.h"#include"malloc.h"
#define ERROR 0
#define OK 0
#define STACKINCREMENT 5
typedef int status;
typedef struct List
{
char *base;
int length;
int listsize;
}List;
typedef struct BiTree
{
BiTree *lchild;
BiTree *rchild;
char data;
}BiTree, *BiTreeP;
typedef struct stack
{
BiTreeP *base;
BiTreeP *top;
int stacksize;
}stack;
status InitList(List *l)
{
if(!(l = (List*)malloc(sizeof(List)))) return ERROR;
l->base = NULL;
l->length = 0;
l->listsize = 0;
}
status scandata(List *l,int n)
{
if(!(l->base = (char*)malloc(n*sizeof(char)))) return ERROR;//程序卡在这一行,给出的错误对话框是:"
char *p = l->base; //Unhandled exception at 0x00f61497 in BiTree.exe: 0xC0000005:
while(p) //Access violation writing location 0x00000000."
{
scanf("%d",p);
p++;
}
}
status InitStack(stack *s,int n)
{
s->stacksize = n;
if(!(s->base = (BiTreeP*)malloc(sizeof(BiTreeP)))) return ERROR;
s->top = s->base;
}
int push(stack *s,BiTree *ch)
{
if(!(s->base + s->stacksize <= s->top))
{
BiTreeP *newbase;
if(!(newbase = (BiTreeP*)realloc(s,(s->stacksize + STACKINCREMENT)*sizeof(BiTreeP))))
return ERROR;
else
{
s->stacksize += STACKINCREMENT;
s->base = newbase;
s->top = s->base + s->stacksize;
}
}
*(s->top++) = ch;
return OK;
}
int StackEmpty(stack *s)
{
if(s->top = s->base) return 1;
else return 0;
}
status main()
{
BiTree *ps = NULL;
stack *s = NULL;
List *l = NULL;
char * pl = NULL;
int n;
InitList(l);
printf("How many data will be inputed ?\n");
scanf("%d",&n);
scandata(l,n);
InitStack(s,n);
if(!(ps = (BiTree*)malloc(sizeof(BiTree)))) return ERROR;
push(s,ps);
while(!StackEmpty(s))
{
while(*pl != '#')
{
if(!(ps->lchild = (BiTree*)malloc(sizeof(BiTree)))) return ERROR;
ps->data = *pl;
push(s,ps);
ps =ps->lchild;
pl++;
}
ps = ps->rchild;
pl++;
}
return OK;
}