Segmentation Fault in Loop's Condition - c++

The following code is to sort a linked list after creating it. The sorting algorithm used is somewhat similar to Bubble Sort. I am checking the two consecutive nodes and swapping them if necessary. I used the debugger which told me that the fault is raised while condition checking for the loops which are used while sorting.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
using namespace std;
struct link_list
{
char value[20];
struct link_list *next;
};
int main()
{
struct link_list *head=NULL;
int i,j;
char input[20];
char ch;
struct link_list *loop_var,*temp2,*prev_node,*temp4=NULL;
temp3=NULL;
do
{
cout<<"\nEnter the string you want to insert";
cin>>input;
cout<<"\nDo you want to continue entering?";
cin>>ch;
if (head==NULL)
{
head=new link_list;
strcpy(head->value,input);
head->next=NULL;
continue;
}
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next);
temp2=new link_list;
loop_var->next=temp2;
strcpy(temp2->value,input);
temp2->next=NULL;
}while(ch=='y' || ch=='Y');
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<loop_var->value<<"\n";
}
cout<<loop_var->value<<"\n";
char arr[20];
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<"\nLoop1";
for (temp4=head;temp4->next!=NULL;temp4=temp4->next)
{
cout<<"\nLoop2";
temp2=temp4;
if (strcmp(temp2->value,temp2->next->value)>0)
{
cout<<"\nSwap Enter";
if (temp2==head && temp2->next->next==NULL)
{
cout<<"\nSpecial1";
temp2->next->next=temp;
temp2->next=NULL;
}
else if (temp2==head)
{
cout<<"\nSpecial2";
head=temp2->next;
temp2->next=head->next;
head->next=temp2;
}
else if (temp2->next->next==NULL)
{
cout<<"\nSpecial3";
prev_node->next=temp2->next;
prev_node->next->next=temp2;
temp2->next=NULL;
}
else
{
cout<<"\nNormal1";
prev_node->next=temp2->next;
temp2->next=prev_node->next->next;
prev_node->next->next=temp2;
cout<<"\nNormal2";
}
}
prev_node=temp4;
cout<<"\nLoop2PreExit";
fflush(stdin);
cout<<"\nLoop2Exit";
}
cout<<"\nLoop1Exit";
}
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<loop_var->value<<"\n";
}
cout<<loop_var->value;
getch();
}

temp2->next->next=temp;
"temp" is not defined anywhere... if your compiler filled in that hole for you, then this is what is causing your loop's condition to segfault.
Also, naming every other variable "temp#" is an easy way to have mistakes like this.

Related

error while passing values in push operation in stack implementation(Parenthesis matching)

#include <iostream>
#include<string>
using namespace std;
class Stack{
public:
int top;
int size;
string *s;
void isBalanced(Stack *st,string exp);
};
push function is
void push(Stack *st,string x){
if(st->top==st->size-1)
cout<<"Stack full\n";
else{
st->top++;
st->s[st->top]=x;
}
}
pop function
string pop(Stack *st){
string x;
if(st->top==-1)
cout<<"Stack is empty\n";
else{
x=st->s[st->top--];
}
return x;
}
errored function
void Stack::isBalanced(Stack *st,string exp){
for(int i=0;exp[i]!='\0';i++){
if(exp[i]=='('){
push(st,exp[i]);
}else if(exp[i]==')'){
if(top==-1)
cout<<"stack empty\n";
pop(st);
}
}
if(top==-1)
cout<<"Balanced\n";
else
cout<<"Not balanced\n";
}
in main
int main()
{
Stack st;
string expression;
cout<<"ENTER EXPRESSION TO CHECK PARENTHESIS BALANCED OR NOT : ";
cin>>expression;
st.size=expression.length();
st.top=-1;
st.s=new string[st.size];
st.isBalanced(&st,expression);
}
here i was trying to implement parenthesis matching problem using in c++ but in the line below code throws error in function isBalanced please
try
to fix
my problem
push(st,exp[i]);
this line throws error canot covert something... and i cant able to fix it.I had donr all posible ways and cant be rectified so ...
push(st, std::to_string(exp[i])); should fix your problem!

How to use Doubly Linked List with Dynamic Array?

My homework is about making a schedule with doubly-linked list. We can create a dynamic array for keeping days. But every day has to have a doubly-linked list which contains time slots. Vectors and arrays are forbidden from use, instead of linked lists. I have difficulty about functions.
This is my header file:
#ifndef _SCHEDULE_H
#define _SCHEDULE_H
#include <string>
using namespace std;
struct Node
{
string courseName;
int time;
Node *next; //forward direction
Node *prev; //backward direction
Node::Node() {}
Node::Node(const string &cName,const int&time, Node * pRight, Node * pLeft)
: courseName(cName),time(time),next(pRight), prev(pLeft)
{}
};
class Schedule
{
public:
Schedule(); //Constructor
//adding new course depend on time
void addCourse(string courseName, char day, int time,Node *Days[6]);
// delete course depend on time
void deleteCourse(char day, int time,Node *Days[6]);
// display a particular course's time
void displayCourse(string courseName,Node *Days);
//prints schedule
void print(Node *Days);
private:
Node *head; //Head node, start of a linked list based on Day
Node *tail; //Tail node, end of a linked list based on Day
};
#endif
Here's my implementation file:
#include <iostream>
#include "Schedule.h"
using namespace std;
Schedule::Schedule()
{
head=new Node(" ",0,NULL,NULL);
tail = NULL;
}
void Schedule::addCourse(string courseName, char day, int time,Node *Days[6])
{
int i;
if (day=='M')
{i=0;}
else if(day=='T')
{i=1;}
else if(day=='W')
{i=2;}
else if(day=='R')
{i=3;}
else if(day=='F')
{i=4;}
else if(day=='S')
{i=5;}
Node*cur=Days[i]->next=head;
if(Days[i]->next==NULL)
{
Days[i]=new Node;
Days[i]->next->courseName=courseName;
Days[i]->time=time;
Days[i]->next=NULL;
Days[i]->prev=NULL;
cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
}
else if(time<Days[i]->next->time && time!=Days[i]->next->time)
{
Node*newcourse=new Node;
//Days[i]=new Node;
Days[i]->next->courseName=courseName;
Days[i]->next->time=time;
Days[i]->next=head;
Days[i]->prev=NULL;
Days[i]->next=newcourse;
cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
}
else if(time>Days[i]->next->time)
{
while(Days[i]->next!=NULL && Days[i]->next->time<time && Days[i]->next->time!=time)
{
Days[i]->next=Days[i]->next->next;
}
if(Days[i]->next->time==time)
{
cout<<"Time conflict"<<endl;
}
else
{
Node*newcourse=new Node;
Days[i]->next->courseName=courseName;
Days[i]->next->time=time;
Days[i]->next=Days[i]->next->next;
Days[i]->prev=Days[i]->next;
Days[i]->next->next=newcourse;
cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
}
}
}
void Schedule::deleteCourse(char day, int time,Node *Days[6])
{
int d;
if (day=='M')
{d=1;}
else if(day=='T')
{d=1;}
else if(day=='W')
{d=2;}
else if(day=='R')
{d=3;}
else if(day=='F')
{d=4;}
else if(day=='S')
{d=5;}
Node*cur=Days[d]->next=head;
if(Days[d]->next==NULL)
{
cout<<"Schedule is empty for this day"<<endl;
}
else
{
}
}
void Schedule::displayCourse(string courseName,Node *Days)
{
}
void Schedule::print(Node *Days)
{
}
Here is my main:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Schedule.h"
using namespace std;
Node *Days = new Node[6];
void CoutSelection()
{
cout<<endl<<endl;
cout<<"Welcome to Schedule Maker. Please select an option:"<<endl;
cout<<" 1) Load the course schedule from a known file"<<endl;
cout<<" 2) Add a time slot manually"<<endl;
cout<<" 3) Remove a time slot manually"<<endl;
cout<<" 4) Print a particular course's time slot"<<endl;
cout<<" 5) Print all schedule"<<endl;
cout<<" 6) Exit" <<endl;
cout<<endl;
cout<<" Please enter your selection as 1-2-3-4-5-6"<<endl;
cout<<endl;
}
int main()
{
int selection;
CoutSelection();
cin>>selection;
Schedule list;
while (selection!=6)
{
if (selection==1)
{ string fileName;
cout<<"Please enter the filename that you want to load"<<endl;
cin>>fileName;
ifstream input;
input.open(fileName);//open file
if(!input.is_open())//control if correctly open
{
cout<<"Couldn't open input file: "<<fileName<<endl;
}
else
{
string cname,line; //course name and day identifier
char day;
int time; //time
while(!input.eof())
{getline(input, line);
stringstream ss(line);
int num;
ss>>cname>>day>>num;
list.addCourse(cname,day,time,*Days[6]);
}
}
}
else if (selection==2)
{
int timeAdded;
string cnameAdded;
char dayAdded;
cout<<"Please enter course name,day and it's time that you want to add like : coursename dayidentifier time"<<endl;
cout<<"Enter the day as M/T/W/R/F/S. (MONDAY:M, TUESDAY:T, WEDNESDAY:W, THURSDAY:R, FRIDAY:F, SATURDAY:S)"<<endl;
cin>>cnameAdded>>dayAdded>>timeAdded;
list.addCourse(cnameAdded,dayAdded,timeAdded,*Days[6]);
}
else if(selection==3)
{
char dayDeleted;
int timeDeleted;
cout<<"Please enter the day and time that you want to delete like : dayidentifider time"<<endl;
cout<<"Enter the day as M/T/W/R/F/S. (MONDAY:M, TUESDAY:T, WEDNESDAY:W, THURSDAY:R, FRIDAY:F, SATURDAY:S)"<<endl;
cin>>dayDeleted>>timeDeleted;
list.deleteCourse(dayDeleted,timeDeleted,*Days[6]);
}
else if(selection==4)
{
string coursedisplayed;
cout<<"Please enter course name that you want to display"<<endl;
cin>>coursedisplayed;
list.displayCourse(coursedisplayed,*Days);
}
else if(selection==5)
{
list.print(*Days);
}
CoutSelection();
cin>>selection;
}
return 0;
}
What is wrong with my code? If I handle one of the functions, I'm sure I can do other functions.
Errors :
error C2664: 'Schedule::addCourse' : cannot convert parameter 4 from 'Node' to 'Node *[]'
IntelliSense: no operator "*" matches these operands
operand types are: * Node
Aside from all the problems presented by #WhozCraig, which I think you should tackle for your own good. Your compiler is talking to you, and it is telling you that your addCourse method receives a pointer to a Node Array.
But in your main you called it with the following list.addCourse(cname,day,time,*Days[6]);. By doing *Days[6] you are telling the method you want to send what is pointed by Days[6]. Thus your compiler is receiving a Node object and not a pointer to a node array.
Try it with the following list.addCourse(cname,day,time,Days);, this will send the pointer to the first element in days.
One pointer to keep in mind, which you'll teacher will likely notice:
You have memory leaks, which is another VERY important subject.

Incorrect outputs for simple solution

Codeforces problem 373A-http://codeforces.com/problemset/problem/373/A
Instead of multiple if statements for counting number of elements of each number,I have tried to check for given condition after sorting the array.I am getting incorrect output for 1st test case(given in the link for the problem).What is wrong with my approach?What should I change in my solution.
My solution:
#include<iostream>
#include<cstring>
using namespace std;
int k,i,j,a;
char panel[17],temp,output[4];
int main()
{
cin>>k;
for(i=0;i<16;i++)
cin>>panel[i];
for(i=0;i<16;i++) //Bubble sort.
{
for(j=0;j<(15-i);j++)
{
if(panel[j]>panel[j+1])
{
temp=panel[j+1];
panel[j+1]=panel[j];
panel[j]=temp;
}
}
}
a=1;
strcpy(output,"YES");
for(i=0;i<16;i++)
{
if(panel[i]==panel[i+1])
++a;
else
a=1;
if(a>(2*k));
{
strcpy(output,"NO");
break;
}
}
cout<<output;
}
You have a semi-colon after the if statement:
if(a>(2*k));
That means, it's always going copy "NO" and break the loop. Remove it.

How store class objects in vector<vector<class *>> and access and pass them to function either by reference or value

#ifndef BINARY_TREE_H
#define BINARY_TREE_H
#include<iostream>
#include<vector>
using namespace std;
class Binary_Tree;
static int levelCount=0;
extern vector<vector<Binary_Tree*>> vec;
extern vector<Binary_Tree*> tempVec;
class Binary_Tree
{
public:
Binary_Tree()
{
childNum=0;
data=0;
level=0;
prev=NULL;
next[0]=NULL;
next[1]=NULL;
};
Binary_Tree(int d)
{
childNum=0;
data=d;
level=0;
prev=NULL;
next[0]=NULL;
next[1]=NULL;
levelCount++;
}
void insert_node(int,int,int);
int get_level();
int get_childCount();
friend int set_childNum(Binary_Tree*);
private:
int childNum;
int data;
int level;
Binary_Tree *prev;
Binary_Tree *next[2];
};
#endif // BINARY_TREE_H
Here is the implementation file
#include<iostream>
#include<cmath>
#include "Binary_Tree.h"
using namespace std;
void Binary_Tree::insert_node(int lev, int d, int sib)
{
if(vec.empty())
{
cout<<"You Have to create Root first";
}
else
{
if(set_childNum(vec[lev][sib-1])==0)
{
cout<<"Child cant be created parent Node already has two childs.";
}
else
{
childNum=set_childNum(vec[lev][sib-1]);
data=d;
level=lev+1;
prev=vec[lev][sib];
next[0]=NULL;
next[1]=NULL;
tempVec.clear();
for(int i=0; i<pow(2,(lev+1)); i++)
{
if(i==childNum-1)
{
tempVec.push_back(this);
}
else
tempVec.push_back(vec[lev][i]);
}
vector<vector<Binary_Tree*>>::iterator itr=vec.begin()+(lev+1);
vec.erase(itr);
vec.insert(itr,tempVec);
}
}
}
int set_childNum(Binary_Tree *lstNdAdr)
{
if(lstNdAdr->get_childCount()==0)
return 1;
else if(lstNdAdr->get_childCount()==1)
return 2;
else
return 0;
}
int Binary_Tree::get_level()
{
return level;
}
int Binary_Tree::get_childCount()
{
if(next[0]==NULL)
{
return 0;
}
else if(next[0]!=NULL && next[1]==NULL)
{
return 1;
}
else
{
return 2;
}
}
MAIN.cpp
#include <iostream>
#include<cmath>
#include"Binary_Tree.h"
using namespace std;
vector<vector<Binary_Tree*>> vec;
vector<Binary_Tree*> tempVec;
int main()
{
Binary_Tree tree;
here:
cout<<"Enter your Choice:1.Create Root Of Tree\n"
<<"2.Insert node\n"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1:
{
int d;
cout<<"Enter Data to insert: ";
cin>>d;
Binary_Tree treeDummy(d);
tree=treeDummy;
tempVec.push_back(&tree);
vec.push_back(tempVec);
}
break;
case 2:
{
int lev;
int sibbling;
int d;
cout<<"Enter at which level and data and parent's sibling-no.: ";
cin>>lev;
cin>>d;
cin>>sibbling;
if(sibbling>pow(2,lev))
cout<<"Illegal Sibbling Number."<<endl;
else
tree.insert_node(lev,d,sibbling);
}
break;
}
int x;
cin>>x;
if(x==5)
{
cout<<endl<<endl;
goto here;
}
return 0;
}
in above code i am trying to create a binary tree type structure which can be manipulated and traversed dynamically that is any node can be inserted and can be removed at run time (although its incomplete because i am stuck at a problem). While pushing back the tempVec vector the code produces a segmentation fault and i am also doubtful in passing the object stored in vetcor> vec to the functions in the implementation (I am new to Stl and first time dealing with vector of vectors containing pointer to the class types)
The nested vector's entries are only filled if i is set to 1. But you attempt to access its element [0][0] regardless. You have out of bounds access when i is not 1.
There are numerous problems present in your code, that and combined with the poor style and formatting makes it not so fun to debug.
Binary_Tree treeDummy(d);
tree = treeDummy;
tempVec.push_back(&tree);
I'm not sure what you're trying to do here but the above looks wrong. You are shallow copying treeDummy's data over to tree. You'll lose the link to whatever child node tree points to. Afterwards you're pushing that same tree instance into your temporary vector. That means all the elements in your vector ends up pointing to the local variable tree in main. So even if no segfault occurred you would run into aliasing problems since they all refer to the same tree object and not a separate unique BinaryTree instance.
vector< vector<Binary_Tree*> >::iterator itr=vec.begin()+(lev+1);
vec.erase(itr);
vec.insert(itr,tempVec);
Your BinaryTree::insert_node is using an invalidated iterator after performing erase which is undefined behavior.
childNum = set_childNum(vec[lev][sib-1]);
// ...
prev = vec[lev][sib];
The above can access an out-of-bound index in your vector. eg. You push_back a tempVec with only 1 element in it and then call insert_node with sib = 1.
// ...
if(x == 5)
{
cout<<endl<<endl;
goto here;
}
The use of goto is also completely unnecessary here and should be replaced with a traditional while loop that checks for condition != 5.
The higher level problem in your program, however, is that there's no clear constraints and invariants in its design. What assumptions and preconditions do each of those functions need to work? Why use vectors to hold BinaryTree nodes when the class itself should be dealing with that. You should get the overall design sorted out first, otherwise you'll just play whack-a-mole as other bugs crop up.

ternary tree giving error

This is simple ternary tree structure . I have written code correctly but while running it says after some time:
Sorry ternary.exe has stopped working.
Can you tell me the cause of this error.
#include<iostream>
#include<string>
using namespace std;
struct tnode{
int data[2];
tnode *ptr[3];
};
void swap(int *a,int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
//for initializing tnode variables as null or null character
void newtree(tnode *&T){
T->data[0]='\0';
T->data[1]='\0';
T->ptr[0]=NULL;
T->ptr[1]=NULL;
T->ptr[2]=NULL;
}
void fillto(tnode *&T,int a){
if(T->data[0]=='\0'){
T->data[0]=a;
}
else if(T->data[0]!='\0'&&T->data[1]=='\0'){
T->data[1]=a;
if(T->data[0]>T->data[1])
swap(T->data[0],T->data[1]);
}
else{
if(a<T->data[0]){
if(T->ptr[0]==NULL){
T->ptr[0]=new(tnode);
newtree(T->ptr[0]);
}
fillto(T->ptr[0],a);
}
else if(a>T->data[1]){
if(T->ptr[2]==NULL){
T->ptr[2]=new(tnode);
newtree(T->ptr[2]);
}
fillto(T->ptr[2],a);
}
else{
if(T->ptr[1]==NULL){
newtree(T->ptr[1]);
T->ptr[1]=new(tnode);
}
fillto(T->ptr[1],a);
}
}
}
tnode *datatnode(string s){
int l=0;
tnode *T;
tnode *E;
T=new(tnode);
char c[0];
newtree(T);
E=T;
while(l<=s.length()){
c[0]=s[l];
cout<<atoi(c)<<endl;
fillto(T,atoi(c));
l++;
}
return E;
}
int main(){
string s="5398124";
tnode *T;
T=new(tnode);
T=datatnode(s);
cout<<T->data[0];
return 0;
}
You should remove '=' sign as below
tnode *datatnode(string s){
int l=0;
tnode *T;
tnode *E;
T=new(tnode);
char c;
newtree(T);
E=T;
int a = s.length();
while(l<a){
c=s[l];
cout<<atoi(&c)<<endl;
fillto(T,atoi(&c));
l++;
}
return E;
}
Its difficult to say from your code (as mentally you have to run it in your head). Better to debug it out. Call some debug at key points in your code and try to locate the exact line of code.... this could produce a lot of debug depending how big your data-set is.
At a guess I would say that you probably hit a bad address or somthing like this, that is usually why programs die un-expectedly and immediatly! So I would suggest being very secure on your pointer checking. For example:
void fillto(tnode *&T,int a){
if (T != NULL){
if(T->data[0]=='\0')
{
T->data[0]=a;
}
:
:
}
else
{
printf("Warning: NULL pointer!\n");
}
}
Basically any time you use a pointer that is passed in to a function you should check it is not null. This is generally good code practise and may help you to find your bugs :)
Also int initialisation can just be:
int i = 0;
instead of
int i = '\0';
The fundamental flaw that causes the error is in the 'void fillto(tnode *&T,int a)' function:
...
if(T->ptr[1]==NULL){
newtree(T->ptr[1]);
...
}
As the function newtree does not check if the pointer is null, you end up dereferencing a NULL pointer in newtree