How Do I Spot a Segmentation Fault?

Posted 12 September 2019 - 02:06 PM

Hi all,
I'm writing this code for a lab, but when I run the program it exits because of segmentation fault. I've searched online on what a segmentation fault is, which is the program trying to access something that shouldn't be accessed. However, I don't think i'm accessing anything incorrectly.
Main.cpp
#include <iostream>
#include <string>
#include "Circle.h"

using namespace std;
int main() {
Circle circleOne;
Circle circleTwo(0, 0, 4);
Circle circleThree(-2, -9, 6);

cout << "Created circleOne" << "("<<circleOne.getX()<<","<<circleOne.getY()<<"):"<<circleOne.getRadius()<<endl;
cout << "Created circleTwo" << "("<<circleTwo.getX()<<","<<circleTwo.getY()<<"):"<<circleTwo.getRadius()<<endl;

cout<<"Created circleThree"<<circleThree.toString() <<endl;
//cout<<"The area of circleOne is "<< circleOne.getArea()<<" Its circumference is "<<circleOne.getCircumference() <<endl;
//cout<<"The area of circleTwo is "<< circleTwo.getArea() <<" Its circumference is "<<circleTwo.getCircumference()<<endl;
//cout<<"The area of circleThree is "<<circleThree.getArea()<<" Its circumference is "<<circleThree.getCircumference()<<endl;

cout << "The distance between two circles is: " << circleTwo.getDistance(circleThree) <<endl;
if(circleOne.intersects(circleTwo) == false){
cout<<"The circles do intersect"<<endl;
}else{
cout<<"The circles do not intersect"<<endl;
}

Circle circleFour(circleOne.resize(40));
cout << circleFour.toString();
}

Circle.cpp
#include <iostream>
#include <iomanip>
#include "Circle.h"
#include "cmath"
#include <string>
using namespace std;
Circle::Circle(){
x = 0;
y = 0;
}

Circle::Circle(int x, int y, int radius){
this->x = x;
this->y = y;

}

int Circle::getX(){
return x;

}

int Circle::getY(){
return y;
}

}

void Circle::setX(int newX){
x = newX;
}

void Circle::setY(int newY){
y = newY;

}
}

double Circle::getArea(){

}

double Circle::getCircumference(){
return 2* M_PI * radius;

}
string Circle::toString(){
return "("+to_string(getX()) +","+ to_string(getY())+ "):"+ to_string(getRadius());
}

double Circle::getDistance(Circle other){
return sqrt( pow( (x-other.getX()), 2)  + pow( (y-other.getY()), 2)) ;

}
bool Circle::intersects(Circle other){
}

void Circle::resize(double scale){
}

Circle Circle::resize(int scale)
{
Circle rCircle;
rCircle.resize(scale);
return rCircle;
}

Circle.h
#include <string>
using namespace std;
class Circle{

private:
int x;
int y;

public:
Circle();

Circle(int x, int y, int radius);

int getX();

int getY();

void setX(int newX);

void setY(int newY);

double getArea();

double getCircumference();
string toString();

double getDistance(Circle other);
void moveTo(int newX, int newY);
bool intersects(Circle other);
void resize(double scale);
Circle resize(int scale);

};

Is This A Good Question/Topic? 0

Replies To: How Do I Spot a Segmentation Fault?

#2 jimblumberg Reputation: 5761
• Posts: 17,640
• Joined: 25-December 09

Re: How Do I Spot a Segmentation Fault?

Posted 12 September 2019 - 02:15 PM

Quote

How Do I Spot a Segmentation Fault

Normally you run the program with your debugger. The debugger should be able to tell you exactly where it detects the problem and then you can view the contents of the variables at the time of the crash.

Edit: Why are you recursively calling resize() in your resize(int) member function?

Jim

This post has been edited by jimblumberg: 12 September 2019 - 02:26 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }