Segmentation fault (core dumped) ????
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
int w=0;
struct node *push(int i) {
w+=1;
struct node *head,*end,*p1,*p2;
p1=(struct node *)malloc(sizeof(struct node ));
p2=(struct node *)malloc(sizeof(struct node ));
head=(struct node *)malloc(sizeof(struct node ));
end->next=NULL;
p1->num=i;
if(w==1)p1=end;
else p2=p1;
head=p2;
return head;
}
void pop(struct node *p)
{
while(p!=NULL)
{
printf("%d",p->num);
p=p->next;
}
}
void main()
{
int n=0;
struct node ww;
struct node *p1,*p2,*head;
p1=(struct node *)malloc(sizeof(struct node ));
p2=(struct node *)malloc(sizeof(struct node ));
head=NULL;
scanf("%d",&p1->num);
while(p1->num!=0){
n+=1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct node *)malloc(sizeof(struct node ));
scanf("%d",&p1->num);
}
p2->next=NULL;
p1=head;
while(p1!=NULL){
head=push(p1->num);
printf("%d\n",p1->num);
p1=p1->next;
}
pop(head);
}
//input 1 2 3 4 0
//output 1 2 3 4
// Segmentation fault (core dumped) ??????
//linxu gcc
[解决办法]
基本上是指针问题,单步调试吧
[解决办法]
进程意外退出会在当前目录下产生形如‘core.数字’的文件比如‘core.1234’
使用命令
gdb 运行程序名 core.数字
进入gdb然后使用bt命令
可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。
[解决办法]
如果只是实现
//input 1 2 3 4 0
//output 1 2 3 4 这样的功能,push和pop这两个函数根本不需要;
struct node *push(int i) 函数不知道楼主要表达什么意思?
空间申请记得释放;
#include<stdio.h>#include<stdlib.h>struct node{ int num; struct node *next;};int w=0;struct node *push(int i) { w+=1; struct node *head,*end,*p1,*p2; p1=(struct node *)malloc(sizeof(struct node )); p2=(struct node *)malloc(sizeof(struct node )); head=(struct node *)malloc(sizeof(struct node )); end=(struct node *)malloc(sizeof(struct node ));//end 只是一个指针,赋值前申请空间 end->next=NULL; p1->num=i; if(w==1)p1=end;//申请多于空间 else p2=p1;//申请多于空间 head=p2; return head;}void pop(struct node *p){ struct node *p1 = p, *p2 = p; while(p1 != NULL) { //printf("%d",p->num); p2 = p1; p1 = p1->next; free( p2); } p1 = NULL; p2 = NULL;}void main(){ int n=0; struct node ww; struct node *p1,*p2,*head; p1=(struct node *)malloc(sizeof(struct node )); //p2=(struct node *)malloc(sizeof(struct node )); head=NULL; scanf("%d",&p1->num); while(p1->num!=0) { n+=1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1 = NULL; p1=(struct node *)malloc(sizeof(struct node )); scanf("%d",&p1->num); } p2->next=NULL; p1=head; while(p1!=NULL){ //head=push(p1->num); printf("%d\n",p1->num); p1=p1->next; } head = NULL; //pop(head);}//input 1 2 3 4 0 //output 1 2 3 4 // Segmentation fault (core dumped) ??????//linxu gcc
[解决办法]
#include<stdio.h>#include<stdlib.h>struct node{ int num; struct node *next;};void pop(struct node *p){ struct node *p1 = p, *p2 = p; while(p1 != NULL) { //printf("%d",p->num); p2 = p1; p1 = p1->next; free( p2); } p1 = NULL; p2 = NULL;}void main(){ int n=0; struct node ww; struct node *p1,*p2,*head; p1=(struct node *)malloc(sizeof(struct node )); //p2=(struct node *)malloc(sizeof(struct node )); head=NULL; scanf("%d",&p1->num); while(p1->num!=0) { n+=1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1 = NULL; p1=(struct node *)malloc(sizeof(struct node )); scanf("%d",&p1->num); } p2->next=NULL; p1=head; while(p1!=NULL){ //head=push(p1->num); printf("%d\n",p1->num); p1=p1->next; } head = NULL; pop(head);}[root@bogon temp]# ./temp1230123
[解决办法]
一般是非法读写内存
[解决办法]
问题在push函数中
指针end并没有初始化
end指向一个随机的地址。然后你修改内存了的值,end->next=NULL;
显然会出现段错误!
[解决办法]
gdb调试一下