读书人

NULL pointer assigment,该怎么处理

发布时间: 2012-05-20 16:03:12 作者: rapoo

NULL pointer assigment

C/C++ code
/* 我想模拟进程的优先级调度,现在只简单做到创建进度这一步,后面的步骤先不用管,这段代码虽然编绎和运行成功,但最后返回"NULL pointer assigment ",搞了2小时始终没想明白哪里错了\ ps.假如这里优先级是0最大, 8最小,不过代码里并没有限定,我的意思就是每输入一个新进和就按进程的优先级从小到大排列,然后返回. 用的是单链的形式,不是顺序表*/#include <stdio.h>#include <string.h>#define MAXSIZE 10typedef struct pcbb{ /* 定义进程  */    char *name;    int priority;    int time;    int state;    struct pcbb *next;}PCB;create(PCB *head){/*  创建进程,带指针的简单 链 */    PCB *temp=NULL,*p,*q;    char *s=NULL;    int i;    head=(PCB*)malloc(sizeof(PCB));    strcpy(head->name,"head");head->priority=-1;head->time=-1;head->state=-1;/*  给头指针赋值,这里只是随便赋下,主要为了防止乱值*/    head->next=NULL;        for(i=1;i<=MAXSIZE;i++){ /* 最多创建10个进程*/        temp=(PCB*)malloc(sizeof(PCB)); /*  新进程建立*/        printf("input the %dth PCB information :name(string) priority(int) time(int) state(int) \n",i);        scanf("%s%d%d%d",temp->name,&temp->priority,&temp->time,&temp->state);/*  输入新进程4大基础信息*/        temp->next=NULL;        printf("you input :%s %d %d %d  \n",temp->name,temp->priority,temp->time,temp->state);/* 查看新进程是否赋值成功 */        p=head->next; /*  p为头指针的下一个指针*/                if (p){/*  如果p不是空的NULL*/            if(temp->priority<p->priority){/*  测试新进程temp优先级<p的优先级吗?  是小于就进行*/                temp->next=p;                head->next=temp;            }            else{/*  不然的话新进程temp优先级>=p的优先级*/                q=p->next;/* p的下一个进程q*/                while (q){/* q不是空的NULL*/                    if (temp->priority<q->priority){/* 测试新进程temp优先级<q的优先级吗?  是小于:找到插入位置准备插入* */                        temp->next=q;                        p->next=temp;                        break;                    }                    p=q;/* 新进程temp优先级>=q的优先级*/                    q=p->next;/* 后移继续寻找位置*/                }                if(q==NULL){/* q是空的NULL,也算是找到位置,用q的前一节p链接上新进程temp*/                    p->next=temp;                    temp->next=NULL;                }            }        }        else{/*  如果p是空的NULL,*/            head->next=temp;temp->next=NULL;        }            printf("anymore? yes or no-:(no to stop,yes to go on)");/*  是否还想继续输入,(输入no以退出for循环)*/        scanf("%s",s);        if( strcmp(s,"no")==0 ) break;            }    }void main(){        PCB *head=NULL;        create(head);        return;        }


[解决办法]
PCB结构体中,char *name没有分配空间,执行strcpy(head->name,"head");当然会出错。
typedef struct pcbb{ /* 定义进程 */
char *name;-->修改为:char name[n];
int priority;
int time;
int state;
struct pcbb *next;
}PCB;
[解决办法]
记住:在使用指针之前,必须让你的指针不能是一个空的。一定要让它指向某内存区域。
[解决办法]
请使用2级指针

create(PCB **head){

相应里面的代码做些修改。
[解决办法]
PCB* create(void)
{
}
[解决办法]
你贴出的代码我也只看到"strcpy(head->name,"head");head->priority=-1;head->time=-1;head->state=-1;/* 给头指针赋值,这里只是随"这一行下面就没看了,至于下面有没有问题,楼主看下就能搞定的
[解决办法]
正好,今天事情做完了:
修改下正确代码如下:
include <string.h>
#include <stdlib.h>

#define MAXSIZE 10

typedef struct pcbb{ /* 定义进程 */
char name[MAXSIZE];
int priority;


int time;
int state;
struct pcbb *next;
}PCB;

void create(PCB *head){/* 创建进程,带指针的简单 链 */
PCB *temp=NULL,*p,*q;
char s[MAXSIZE];
int i;
head=(PCB*)malloc(sizeof(PCB));
strcpy(head->name,"head");head->priority=-1;head->time=-1;head->state=-1;/* 给头指针赋值,这里只是随便赋下,主要为了防止乱值*/
head->next=NULL;

for(i=1;i<=MAXSIZE;i++){ /* 最多创建10个进程*/
temp=(PCB*)malloc(sizeof(PCB)); /* 新进程建立*/
printf("input the %dth PCB information :name(string) priority(int) time(int) state(int) \n",i);
scanf("%s%d%d%d",temp->name,&temp->priority,&temp->time,&temp->state);/* 输入新进程4大基础信息*/
temp->next=NULL;
printf("you input :%s %d %d %d \n",temp->name,temp->priority,temp->time,temp->state);/* 查看新进程是否赋值成功 */
p=head->next; /* p为头指针的下一个指针*/

if (p){/* 如果p不是空的NULL*/
if(temp->priority<p->priority){/* 测试新进程temp优先级<p的优先级吗? 是小于就进行*/
temp->next=p;
head->next=temp;
}
else{/* 不然的话新进程temp优先级>=p的优先级*/
q=p->next;/* p的下一个进程q*/
while (q){/* q不是空的NULL*/
if (temp->priority<q->priority){/* 测试新进程temp优先级<q的优先级吗? 是小于:找到插入位置准备插入* */
temp->next=q;
p->next=temp;
break;
}
p=q;/* 新进程temp优先级>=q的优先级*/
q=p->next;/* 后移继续寻找位置*/
}
if(q==NULL){/* q是空的NULL,也算是找到位置,用q的前一节p链接上新进程temp*/
p->next=temp;
temp->next=NULL;
}
}
}
else{/* 如果p是空的NULL,*/
head->next=temp;temp->next=NULL;
}
printf("anymore? yes or no-:(no to stop,yes to go on)");/* 是否还想继续输入,(输入no以退出for循环)*/
scanf("%s",s);
if( strcmp(s,"no")==0 ) break;

}

}


void main(){
PCB *head=NULL;
create(head);
return;

}
[解决办法]
主要是在未分配的地址空间赋值

(1)

C/C++ code
typedef struct pcbb{    char *name;  /*这里只是个指针,没有内存空间*/    int priority;    int time;    int state;    struct pcbb *next;}PCB;/*所以下面的赋值都会出错*/strcpy(head->name,"head");scanf("%s%d%d%d",temp->name,&temp->priority,&temp->time,&temp->state);/*  输入新进程4大基础信息*/ 

读书人网 >C语言

热点推荐