程序接收信号SIGSEGV,分段故障! !这是什么意思,我该如何解决这个问题? [英] Program received signal SIGSEGV, segmentation fault ! ! what does this mean and how do I solve this?

查看:1294
本文介绍了程序接收信号SIGSEGV,分段故障! !这是什么意思,我该如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
struct node *head=NULL;
static int count=0;

struct node
{
	int coef;
	int pow;
	struct node *link;
};

void showoff()
{
	struct node *trace;
	trace=head;
	printf("\n");
	printf("it's working!!!!!!!!!!!!!!!!'1");
	while(trace!=NULL)
	{
		printf("|  | |  | |%x|-->",trace->link);
		trace=trace->link;
		printf("it's working!!!!!!!!!!!!!!!!'2");
	}
	printf("it's working!!!!!!!!!!!!!!!!'3");
}

void omega(int degree)
{	
	int i;
	struct node *temp,*t;
	temp=head;
	t=head;

	for(i=0;i<=degree;i++)	
	{
		temp=(struct node*)malloc(sizeof(struct node));
		//printf("temp  it's working!!!!!!!!!!!!!!!!'\n");
		if(count==0)
		{
			temp->link=head;
			head=temp;
			count++;
			//printf("count==0 it's working!!!!!!!!!!!!!!!!'\n");
		}
		else if (count==1)
		{
			temp->link=head->link;
			head->link=temp;
			count++;
			//printf("count==1 it's working!!!!!!!!!!!!!!!!'\n");
		}
		else
		{
			printf("else  it's working!!!!!!!!!!!!!!!!'\n");
			while(t->link!=NULL)
			{
				t=t->link;	
				//printf("while loop of else it's working!!!!'\n");
			}	
			temp->link=t->link;
			t->link=temp;	
			count++;	
			//printf("outside else it's working!!!!!!!!!!!!!!!!'\n");
		}
		printf("omega it's working!!!!!!!!!!!!!!!!'\n");
	}	
}

int main()
{
	int opt,d;
	printf("---->!!!OPTIONS!!!<----\n");
	printf("1.Enter first polynomial\n");
	scanf("%d",&opt);
	
	switch(opt)
	{
		case 1:
			printf("Enter the degree of polynomial\n");
			scanf("%d",&d);
			omega(d);
			showoff();
			break;
	}
}





我的尝试:



首先,我写的这段代码是用于创建链表,所以当用户输入no时。节点(这里程序用程度表示)然后程序应该创建相互链接的节点,但是不是那样,当我运行程序时它在输入后停止,它确实感觉它正在运行然后它显示

处理退出4.933秒后返回值3221225477

按任意键继续......



我试着用我疯狂的编写printf语句的方式调试程序,你可以在代码中看到,发现'else'里面的'while循环'不起作用,这就是程序崩溃的原因。谁知道为什么???代码中没有错误。请帮助它让我疯了,谢谢!



What I have tried:

First of all, this code I wrote is for creating linked list,so when user enter no. of nodes(here in the program it's denoted by degree) then the program should create nodes linked with each other, but instead of that, when I run the program it stops after taking inputs, it does feel like it's running and then it shows
"Process exited after 4.933 seconds with return value 3221225477
Press any key to continue . . ."

I tried to debug the program with my crazy style of writing printf statements as you can see in the code and found that 'while loop' which is inside 'else' is not working,and that's the reason why the program is crashing. Anyone knows why??? There is no error in the code . Please help it's driving me nuts, Thanks!

推荐答案

好吧......看看你的代码。

Well... look at your code.
printf("else  it's working!!!!!!!!!!!!!!!!'\n");
	while(t->link!=NULL)
	{
		t=t->link;	    (/*<--------------Here's the culprit*/)
			
	}

但是什么时候开始?您没有将它设置为该代码中的任何值...甚至显示它的定义!因此,除非你在某个时候将它设置为特定值 - 我怀疑你这样做是因为它可能需要在你的for循环中设置 - 它可能指向任何地方。



使用调试器,看看它包含的确切内容。



顺便说一句:给自己一个忙,并正确缩进!这使得它更容易遵循...

But what is in t when it starts? You don't set it to any value in that code ... or even show the definition of it! So unless you set it to a specific value at some point - and I doubt you do because it probably needs to be set inside your for loop - it could be pointing anywhere.

Use the debugger, and see exactly what it contains.

BTW: do yourself a favour and indent that lot correctly! It makes it a load easier to follow...


学习正确缩进代码,它显示其结构,它有助于阅读和理解。它还有助于发现结构错误。

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
#include<stdio.h>
#include<conio.h>
struct node *head=NULL;
static int count=0;

struct node
{
	int coef;
	int pow;
	struct node *link;
};

for(i=0;i<=degree;i++)
{
	temp=(struct node*)malloc(sizeof(struct node));
	//printf("temp  it's working!!!!!!!!!!!!!!!!'\n");
	if(count==0)
	{

		temp->link=head;
		head=temp;
		count++;
		//printf("count==0 it's working!!!!!!!!!!!!!!!!'\n");
	}
	else if (count==1)
	{
		temp->link=head->link;
		head->link=temp;
		count++;
		//printf("count==1 it's working!!!!!!!!!!!!!!!!'\n");
	}
	else
	{
		printf("else  it's working!!!!!!!!!!!!!!!!'\n");
		while(t->link!=NULL)
		{
			t=t->link;	    (/*<--------------Here's the culprit*/)
		}
		temp->link=t->link;
		t->link=temp;
		count++;

		//printf("outside else it's working!!!!!!!!!!!!!!!!'\n");

	}
}



专业程序员的编辑有这个功能和其他功能,如括号匹配和语法高亮。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ] < br $> b $ b


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

#include<stdio.h>
#include<conio.h>
static int count=0;

struct node
{
	int coef;
	int pow;
	struct node *link;
};
struct node *head=NULL; // Always define a structure before using it !

for(i=0;i<=degree;i++) // what is degree, it comes from nowhere
{




引用:

我尝试用我疯狂的写入printf语句来调试程序

I tried to debug the program with my crazy style of writing printf statements



试试调试器,它会让你在代码中看到变量的包含执行。





有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道是什么你的cpde应该这样做,它没有找到错误,只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


Give a try to debugger, it will allow you to see the contain of variable during code execution.


There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于程序接收信号SIGSEGV,分段故障! !这是什么意思,我该如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆