c语言出现Program received signal SIGSEGV, Segmentation fault是什么原因? 20
#include<stdio.h>#include<stdlib.h>inttotal_time=20;inttime_slice=3;typedefstructproc...
#include<stdio.h>
#include<stdlib.h>
int total_time = 20;
int time_slice = 3;
typedef struct process{
char pname[10];//进程名
int WaitTime;//数据
int priority;//数字越大优先级越高
struct process *next;
}PROCESS;
int in_queue(PROCESS *head,PROCESS *p);
void run();
PROCESS *init()
{
int i = 0;
char a;
PROCESS *head_new;
head_new = (struct process*)malloc(sizeof(struct process));
if(!head_new) exit(1);
head_new = NULL;
do
{
struct process *s;
printf("请输入各进程\n");
s = (struct process*)malloc(sizeof(struct process));
if(!s) exit(1);
printf("输入进程名,数据,优先级\n");
scanf("%s",&(s->pname));
scanf("%d",&(s->WaitTime));
scanf("%d",&(s->priority));
s->next = NULL;
in_queue(head_new,s);
i++;
}while(i<3);
return head_new;
}
int in_queue(PROCESS *head,PROCESS *p) //入队函数
{
if(head == NULL)
{
head = p;
p->next = NULL;
}
else
{
p->next = head;
head = p;
}
return 0;
}
void run()
{
PROCESS *p,*q;
p = init();
q = p;
while(p->next!=NULL)
{
if(p->priority>=q->priority)
{
q=p;
p=p->next;
}
else
{
p=p->next;
}
}
printf("输出:%c",q->pname);
}
int main()
{
run();
return 0;
} 展开
#include<stdlib.h>
int total_time = 20;
int time_slice = 3;
typedef struct process{
char pname[10];//进程名
int WaitTime;//数据
int priority;//数字越大优先级越高
struct process *next;
}PROCESS;
int in_queue(PROCESS *head,PROCESS *p);
void run();
PROCESS *init()
{
int i = 0;
char a;
PROCESS *head_new;
head_new = (struct process*)malloc(sizeof(struct process));
if(!head_new) exit(1);
head_new = NULL;
do
{
struct process *s;
printf("请输入各进程\n");
s = (struct process*)malloc(sizeof(struct process));
if(!s) exit(1);
printf("输入进程名,数据,优先级\n");
scanf("%s",&(s->pname));
scanf("%d",&(s->WaitTime));
scanf("%d",&(s->priority));
s->next = NULL;
in_queue(head_new,s);
i++;
}while(i<3);
return head_new;
}
int in_queue(PROCESS *head,PROCESS *p) //入队函数
{
if(head == NULL)
{
head = p;
p->next = NULL;
}
else
{
p->next = head;
head = p;
}
return 0;
}
void run()
{
PROCESS *p,*q;
p = init();
q = p;
while(p->next!=NULL)
{
if(p->priority>=q->priority)
{
q=p;
p=p->next;
}
else
{
p=p->next;
}
}
printf("输出:%c",q->pname);
}
int main()
{
run();
return 0;
} 展开
1个回答
展开全部
段错误,访问非法内存。
更多追问追答
追问
那是在哪个位置出现段错误呢?
追答
em...这我一下也看不出来,等会仔细看看并调试一下
init里面这几句没有什么用a,也没有free。
head_new = (struct process*)malloc(sizeof(struct process));
if(!head_new) exit(1);
head_new = NULL;
这个不对
int in_queue(PROCESS *head,PROCESS *p)
你得用PROCESS **head才行。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询