LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ trying to mutate a part of my class, but get seg faults. (https://www.linuxquestions.org/questions/programming-9/c-trying-to-mutate-a-part-of-my-class-but-get-seg-faults-531181/)

anon276 02-21-2007 08:26 PM

C++ trying to mutate a part of my class, but get seg faults.
 
Hey guys. I'm having problems accessing thing in a class that I made. Code as follows.
Code:


//This class is going to hold all the info that I need to emulate the scheduler.
//More than likely, this will have a vector for itself.
class process
{
public:
      process();                      // Default constructor.
      ~process();                      // Default destructor.
      int getPriority();              // Returns the priority of the process
      int getArr_Time();              // Returns the arrival time of the process
      char getRTP();                  // Returns either r or n for the value.
      int getCPU_Burst(int burst_loc); // Returns the burst time at CPU_Burst[burst_loc]
      int getIO_Burst (int burst_loc); // Returns the bust time at IO_Burst[burst_loc]
      //Mutators
      void setPriority(int pri);      // change value of priority
      void setArr_Time(int arr);      // change value of arrival time
      void setRTP(char rtp);          // change value of RTP via rtp. make distinction
      void setCPU_Burst(int loc, int val); //change the value of CPU_Burst[loc] to val.
      void setIO_Burst(int loc, int val); //change the value of IO_Burst[loc] to val. 
//variables for class so I dont' stupidly play with them.
private:
        int priority;          //the priority it has in the scheduler
        int arr_time;          //when it arrived
        char RTP;              //check for real time process status.
        vector<int> CPU_Burst; //CPU burst. REason for vector is because we
                                // don't know if there will be seperate bursts for
                                // CPU work
        vector<int> IO_Burst;  //Time for IO bursts. Similar reasons for using
                                // a vector.
};
// END OF CLASS OUTLINE


// CLASS FUNCTION DEFINTIONS //
// Default Constructor. Only using default to simplify things.
// All set to incorrect values as this will ensure that I will have to change
// them to make them work right
process::process()
{
  priority = -1;
  arr_time = -1;
  RTP = 'a';
  CPU_Burst[0] = -1;
  IO_Burst[0] = -1;

}

// This simply returns the priority.
int process::getPriority()
{
    return priority;
}
// returns Arrival time of process
int process::getArr_Time()
{
    return arr_time;
}

//returns the character representing weither or not it's a real time process
char process::getRTP()
{
    return RTP;
}

//returns the value of CPU_Burst[burst_loc]
int process::getCPU_Burst(int burst_loc)
{
    return CPU_Burst[burst_loc];
}

//returns the value of IO_Burst[burst_loc]
int process::getIO_Burst(int burst_loc)
{
    return IO_Burst[burst_loc];
}

//Changes the priority of the process
void process::setPriority(int pri)
{
    priority = pri;
}

//Changes Arrival time   
void process::setArr_Time(int arr)
{
    arr_time = arr;
}

//Changes the RTP status
void process::setRTP(char rtp)
{
    RTP = rtp;
}

//changes cpu burst time at loc to val
void process::setCPU_Burst(int loc, int val)
{
//PROBLEM IS HERE:
    CPU_Burst[loc] = val;
}

// changes io burst time at loc to val
void process::setIO_Burst(int loc, int val)
{
//PROBLEM IS ALSO PROBABLY HERE
    IO_Burst[loc] = val;   
}

process::~process()
{//Destructor. add stuff later maybe
}
// END FUNCTION DEFINITONS//

// END PROGRAM

//CLASS CODE HERE!


The code I've got compiles fine. And it runs up until I have this:

Code:

processes[process_count].setCPU_Burst(loc / 2, intbuff);
I've managed to isolate it specifically to when it trys to access the vector CPU_Burst[burst_loc] (and more than likely, with IO_Burst too, as that was copy-pasted from the CPU_Burst code). I'm completely stumped with this one... if someone could offfer some insight as to what I'm doing wrong, it would be appreciated... If I was to take a stab at it though, I'd say that CPU_Burst[loc] doesn't actually exist in the memory, despite the fact I've actually assigned it in the constructor. Thanks. :)

wjevans_7d1@yahoo.co 02-21-2007 08:45 PM

This is obviously part of a kernel, but is there any way you could simplify it, to find the problem more easily?

Maybe see whether you can reproduce the problem as a user process. (This may involve writing some sort of harness around it, which means you'll be adding code before you start removing any.) Then gradually reduce the code in this user process until it's a simple as possible without the problem going away.

Is this possible?

anon276 02-21-2007 09:25 PM

Everything's all in one file. The objective is to write a linux scheduler emulator I narrowed it down to whenever I try to make a reference to the variable CPU_Burst inside the class. I don't know how I'd be going about writing a harness around the code... mainly becuase I have no idea on how to write the harness... ^_^'

graemef 02-21-2007 09:57 PM

Do you check that CPU_Burst.size() > burst_loc before you try and access CPU_Burst[burst_loc] ?

anon276 02-21-2007 10:46 PM

I was thinking about doing that. I just haven't had a chance to implement it. I'll let you know the results as soon as I do.

EDIT: the size is some garbage value in the 870,000 range... Maybe that's why it's faulting out? lol.

EDIT2: Solved by changing vectors into arrays. I just hope there aren't more than 10 data elements, heh.

wjevans_7d1@yahoo.co 02-23-2007 08:37 AM

Uh oh.

Quote:

I just hope there aren't more than 10 data elements, heh.
Unless this is for a class assignment or something, I predict that within a year, this assumption will yield a bug that won't be easy to find.


All times are GMT -5. The time now is 02:05 PM.