求教一个NULL pointer assignment的有关问题
求教一个NULL pointer assignment的问题
代码在下面,所有指针在使用前都已经分配了地址,程序运行结果正确,为什么运行后总有NULL pointer assignment提示?
#include<stdio.h>
typedef int * array;
typedef struct node VNode;
typedef VNode *PNode;
struct node
{
int i;
int j;
int type;
PNode pre;
PNode nex;
};
PNode head=NULL,tail=NULL;
int ability;
int row,col;
array * matrix=NULL;
void error(char *s)
{
printf(s);
getch();
exit(1);
}
void insert_virus(int type, int i, int j)
{
PNode tmp=NULL,ptr=NULL;
tmp=(PNode)malloc(sizeof(VNode));
if(tmp==NULL)
error("Memory allocate error");
tmp->i=i;
tmp->j=j;
tmp->type=type;
tmp->pre=NULL;
tmp->nex=NULL;
if(tail==NULL)
{
tail=tmp;
head=tail;
}
else if(type>=(tail->type))
{
tail->nex=tmp;
tmp->pre=tail;
tail=tmp;
}
else
{
ptr=tail;
while(ptr!=NULL&&(ptr->type)>type)
ptr=ptr->pre;
if(ptr!=NULL)
{
tmp->nex=ptr->nex;
tmp->pre=ptr;
ptr->nex=tmp;
tmp->nex->pre=tmp;
}
else
{
tmp->nex=head;
head->pre=tmp;
head=tmp;
}
}
}
void delete_virus(PNode virus)
{
if(virus==head)
{
head=head->nex;
head->pre=NULL;
}
else if(virus==tail)
{
tail=tail->pre;
tail->nex=NULL;
}
else {
virus->pre->nex=virus->nex;
virus->nex->pre=virus->pre;
}
free(virus);
}
int check_virus(PNode virus)
{
int flag=0;
int i=virus->i, j=virus->j, type=virus->type;
if(i-1>=0&&matrix[i-1][j]<0)
if(-matrix[i-1][j]>ability)
flag=1;
else
{
matrix[i-1][j]=type;
insert_virus(type,i-1,j);
}
if(i+1<row&&matrix[i+1][j]<0)
if(-matrix[i+1][j]>ability)
flag=1;
else
{
matrix[i+1][j]=type;
insert_virus(type,i+1,j);
}
if(j-1>=0&&matrix[i][j-1]<0)
if(-matrix[i][j-1]>ability)
flag=1;
else
{
matrix[i][j-1]=type;
insert_virus(type,i,j-1);
}
if(j+1<col&&matrix[i][j+1]<0)
if(-matrix[i][j+1]>ability)
flag=1;
else
{
matrix[i][j+1]=type;
insert_virus(type,i,j+1);
}
return flag;
}
int main(void)
{
int i,j;
int casenum,result;
int *virustype=NULL;
PNode ptr=NULL,ptr1=NULL;
casenum=0;
ability=0;
while(1)
{
scanf("%d %d",&row,&col);
if(row==0&&col==0)
break;
casenum++;
matrix=(array *)malloc(row*sizeof(array));
if(matrix==NULL)
error("Memory allocate error");
代码在下面,所有指针在使用前都已经分配了地址,程序运行结果正确,为什么运行后总有NULL pointer assignment提示?
#include<stdio.h>
typedef int * array;
typedef struct node VNode;
typedef VNode *PNode;
struct node
{
int i;
int j;
int type;
PNode pre;
PNode nex;
};
PNode head=NULL,tail=NULL;
int ability;
int row,col;
array * matrix=NULL;
void error(char *s)
{
printf(s);
getch();
exit(1);
}
void insert_virus(int type, int i, int j)
{
PNode tmp=NULL,ptr=NULL;
tmp=(PNode)malloc(sizeof(VNode));
if(tmp==NULL)
error("Memory allocate error");
tmp->i=i;
tmp->j=j;
tmp->type=type;
tmp->pre=NULL;
tmp->nex=NULL;
if(tail==NULL)
{
tail=tmp;
head=tail;
}
else if(type>=(tail->type))
{
tail->nex=tmp;
tmp->pre=tail;
tail=tmp;
}
else
{
ptr=tail;
while(ptr!=NULL&&(ptr->type)>type)
ptr=ptr->pre;
if(ptr!=NULL)
{
tmp->nex=ptr->nex;
tmp->pre=ptr;
ptr->nex=tmp;
tmp->nex->pre=tmp;
}
else
{
tmp->nex=head;
head->pre=tmp;
head=tmp;
}
}
}
void delete_virus(PNode virus)
{
if(virus==head)
{
head=head->nex;
head->pre=NULL;
}
else if(virus==tail)
{
tail=tail->pre;
tail->nex=NULL;
}
else {
virus->pre->nex=virus->nex;
virus->nex->pre=virus->pre;
}
free(virus);
}
int check_virus(PNode virus)
{
int flag=0;
int i=virus->i, j=virus->j, type=virus->type;
if(i-1>=0&&matrix[i-1][j]<0)
if(-matrix[i-1][j]>ability)
flag=1;
else
{
matrix[i-1][j]=type;
insert_virus(type,i-1,j);
}
if(i+1<row&&matrix[i+1][j]<0)
if(-matrix[i+1][j]>ability)
flag=1;
else
{
matrix[i+1][j]=type;
insert_virus(type,i+1,j);
}
if(j-1>=0&&matrix[i][j-1]<0)
if(-matrix[i][j-1]>ability)
flag=1;
else
{
matrix[i][j-1]=type;
insert_virus(type,i,j-1);
}
if(j+1<col&&matrix[i][j+1]<0)
if(-matrix[i][j+1]>ability)
flag=1;
else
{
matrix[i][j+1]=type;
insert_virus(type,i,j+1);
}
return flag;
}
int main(void)
{
int i,j;
int casenum,result;
int *virustype=NULL;
PNode ptr=NULL,ptr1=NULL;
casenum=0;
ability=0;
while(1)
{
scanf("%d %d",&row,&col);
if(row==0&&col==0)
break;
casenum++;
matrix=(array *)malloc(row*sizeof(array));
if(matrix==NULL)
error("Memory allocate error");