LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2007, 08:26 PM   #1
anon276
LQ Newbie
 
Registered: Feb 2007
Posts: 11

Rep: Reputation: Disabled
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.
 
Old 02-21-2007, 08:45 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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?
 
Old 02-21-2007, 09:25 PM   #3
anon276
LQ Newbie
 
Registered: Feb 2007
Posts: 11

Original Poster
Rep: Reputation: Disabled
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... ^_^'
 
Old 02-21-2007, 09:57 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Do you check that CPU_Burst.size() > burst_loc before you try and access CPU_Burst[burst_loc] ?
 
Old 02-21-2007, 10:46 PM   #5
anon276
LQ Newbie
 
Registered: Feb 2007
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.

Last edited by anon276; 02-21-2007 at 11:18 PM.
 
Old 02-23-2007, 08:37 AM   #6
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC seg faults benne Linux - Software 5 09-05-2005 01:09 AM
Repeated seg faults Tick Linux - General 5 07-29-2004 07:09 PM
seg faults happening allan_y Linux - General 2 07-25-2004 05:30 AM
bash seg faults Kilka *BSD 4 12-15-2003 01:40 AM
w3m seg faults slakmagik Linux - Software 0 05-04-2003 10:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration