Segmentation fault (core dumped) ????

此间乐不思蜀 2012-07-26 04:09:13
#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



...全文
352 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
此间乐不思蜀 2012-07-27
  • 打赏
  • 举报
回复
其实我是想用链表建立一个堆栈的push()是压栈,pop()是出栈,压栈函数对不???
此间乐不思蜀 2012-07-27
  • 打赏
  • 举报
回复
我的问题没有描述清楚,我是想先建立一个链表,再利用这个链表来建立一个堆栈??求指导
[Quote=]用 4 楼 的回复:]

C/C++ code

#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)
{
……
[/Quote]
此间乐不思蜀 2012-07-27
  • 打赏
  • 举报
回复
一般段错误是什么原因?
[Quote=引用 6 楼 的回复:]

问题在push函数中
指针end并没有初始化
end指向一个随机的地址。然后你修改内存了的值,end->next=NULL;
显然会出现段错误!
[/Quote]
此间乐不思蜀 2012-07-27
  • 打赏
  • 举报
回复
gcc gdb 调试怎么用啊?本人菜鸟一枚,勿喷!
[Quote=引用 8 楼 的回复:]

gdb调试一下
[/Quote]
booirror 2012-07-27
  • 打赏
  • 举报
回复
问题在push函数中
指针end并没有初始化
end指向一个随机的地址。然后你修改内存了的值,end->next=NULL;
显然会出现段错误!
AnYidan 2012-07-27
  • 打赏
  • 举报
回复
一般是非法读写内存
常如意 2012-07-27
  • 打赏
  • 举报
回复
gdb调试一下
夏天__ 2012-07-26
  • 打赏
  • 举报
回复

#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]# ./temp
1
2
3
0
1
2
3
夏天__ 2012-07-26
  • 打赏
  • 举报
回复
如果只是实现
//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

赵4老师 2012-07-26
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生形如‘core.数字’的文件比如‘core.1234’
使用命令
gdb 运行程序名 core.数字
进入gdb然后使用bt命令
可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。
luciferisnotsatan 2012-07-26
  • 打赏
  • 举报
回复
基本上是指针问题,单步调试吧

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧