1 Replies - 272 Views - Last Post: 12 September 2019 - 02:15 PM Rate Topic: -----

#1 Adriel16   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 21-May 13

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;
  radius = 1;
}

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

  int Circle::getX(){
    return x;
  
  }
  
  int Circle::getY(){
    return y;
  }
  
  int Circle::getRadius(){
    return radius;
  }
  
  void Circle::setX(int newX){
    x = newX;
  }
  
  void Circle::setY(int newY){
    y = newY;
  
  }
  void Circle::setRadius(int newRadius){
    radius = newRadius;
  }
  
  double Circle::getArea(){
    return M_PI * radius * radius;

  }

  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){
    return getRadius() + other.radius >= getDistance(other);
  }

  void Circle::resize(double scale){
    radius = radius * 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;
  int radius;
  
  public:
  //default constructor header
  Circle();
  
  //regular constructor header
  Circle(int x, int y, int radius);

  
  int getX();
  
  int getY();
  
  int getRadius();
  
  void setX(int newX);
  
  void setY(int newY);
  void setRadius(int newRadius);
  
  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   User is online

  • member icon

Reputation: 5761
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1