Click here to Skip to main content
15,921,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I tried to debug this program it shows Program received signal SIGSEGV,Segmentation fault. I don't know what to do that's why I posted this question.

What I have tried:

C++
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
static int count=0;
struct node
{
	int coef;
	int pow;
	struct node *link;
};

struct node *head=NULL;

void showoff()
{
	struct node *t1;
	t1=head;
	while(t1!=NULL)
	{
		printf("|%d|%d|%x|--",t1->coef,t1->pow,t1->link);
		t1=t1->link;
	}
}

int main()
{
	int n,i=0;
	struct node *temp,*t;
	t=head;
	printf("Number of nodes\n");
	scanf("%d",&n);
	
	while(i<n)
	{
		temp=(struct node*)malloc(sizeof(struct node));
		temp->coef=NULL;
		temp->pow=NULL;
		
		if(count==0)
		{
			temp->link=head;
			head=temp;
		}
		if(count==1)
		{
			temp->link=head->link;
			head->link=temp;
		}
		if(count>1)
		{
			while(t->link!=NULL)    /*Responsible for error. Can you explain why*/
			{
				t=t->link;
			}
			temp->link=t->link;
			t->link=temp;
		}
		count++;
		i++;
	}
	showoff();
}
Posted
Updated 23-Aug-18 8:25am
v2

Firstly, you should use a debugger to fix this. It will show you quickly exactly what the problem is. If you do not have one with your compiler then get one that does. Visual Studio 2017 community edition is available at no cost ie., free.

My guess is the fault is caused because t is invalid and you are deferencing an invalid pointer.

Try adjusting your loop so you display things in every iteration :
C++
// ...
	for( i = 0; i < n; ++i )    // I prefer a for loop for this
	{
		// ... logic goes here

		printf( "i is %d, count is %d\n", i, count );
		showoff();
		count++;
	}
 
Share this answer
 
v4
Comments
Member 13954179 23-Aug-18 12:58pm    
Yeah I used debugger and it show that there is some problem with"while(t->link!=NULL)".
Error message "Program received signal SIGSEGV,Segmentation fault"
Member 13954179 23-Aug-18 13:07pm    
So I got it that the problem is with t pointer, but now what. I literally don't what do it. I mean logically the program is right
Rick York 23-Aug-18 13:23pm    
At this point you have to step through the program and watch everything it does so you can see where it becomes invalid. If that is a some time into its running then you might want to add trace or printf statements to display important information. This is one of the hardest things to learn about programming and that is debugging what you have written. Eventually you will learn techniques that help you write code that is easier to debug.
Rick York 23-Aug-18 13:28pm    
You have the showoff function. I would move that so it displays things after each iteration so you can make you are correct with each one. Also display the counter while you are at it. These things well help you see what is happening better and that is the key to debugging : see what is going on with all of your data. I will adjust my first solution so you can see what I mean a little better.
Member 13954179 23-Aug-18 13:46pm    
Thanks at last I got it
Try breaking the following line into two lines:
C++
struct node *temp,*t;


Make it:
C++
struct node *temp;
struct node *t;


Just try that and rebuild and run see what happens.
I'm wondering if the struct * isn't being declared properly.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900