Reputation: 47
The program is supposed to allocate memory as an operating system would do.
This is the main function
int main (int argc, char* argv[]){
string command;
if(argc==2)
command = argv[1];
else cout<<"Not enough commands"<<endl;
if (command.compare("best"))
cout<<"Using best fit algorithm"<<endl;
if (command.compare("worst"))
cout<<"Using worst fit algorithm"<<endl;
cout<<"\t1.Add Program\n";
cout<<"\t2.Kill Program\n";
cout<<"\t3.Fragmentation\n";
cout<<"\t4.Print Memory\n";
cout<<"\t5.Exit\n";
LinkedList Memory;
Memory.createMemory();
int choice;
cin>>choice;
cout<<"choice - "<<choice<<endl;
if (choice==1){
string programName;
cin>>programName;
cout<<"Program name - "<<programName<<endl;
int si;
cin>>si;
cout<<"Program size (KB) - "<<si<<endl;
Memory.addProgram(si, programName);
}
if (choice==2){
string programName;
cin>>programName;
cout<<"Program name - "<<programName<<endl;
Memory.killProgram(programName);
}
if (choice==4){
Memory.print();
}
if (choice==5){
return 1;
}
return 0;
}
And this is the linkedlist class with its functions
class LinkedList{
private:
struct node{
string name;
node *next;
};
typedef struct node * nodePointer;
nodePointer head;
public:
void createMemory();
void addProgram(int val, string s);
void killProgram(string s1);
void print();
void fragmentation();
LinkedList(){head=NULL;}
};
void LinkedList::createMemory(){
int i=0;
node* temp=head;
while(i<32){
temp->name="Free";
temp=temp->next;
i++;
}
};
void LinkedList::addProgram(int val, string s){
int i=0;
node* temp=head;
while(temp->name!="Free")
temp=temp->next;
while(temp->name=="Free"){
while (i<val){
temp->name=s;
temp=temp->next;
i++;
}
}
};
void LinkedList::killProgram(string s){
node* temp=head;
while(temp->name!=s)
temp=temp->next;
while(temp->name==s)
temp->name="Free";
};
void LinkedList::print(){
node*temp=head;
int i=0;
while(i<32){
cout<<temp->name<<"\t";
temp=temp->next;
if ((i+1)%8==0){
cout<<endl;
}
i++;
}
};
Whenever I call one of the class functions I get a run time error and i don't understand why
Upvotes: 1
Views: 436
Reputation: 1250
When you invoke Memory.createMemory();
, this program will crash.
Because you didn't allocate memory for temp
in the function createMemory()
, temp
points to NULL, so temp->name="Free";
will crash your program. You should use new
to allocate memory for temp
. You should try this site to help you.
BTW, the way you treat the command parameter is not appropriate, what if the user input more than 2 parameters, and you tell the user there is Not enough commands
.
if(argc==2)
command = argv[1];
else cout<<"Not enough commands"<<endl;
Upvotes: 0
Reputation: 667
LinkedList memory;
memory.createMemory();
This code is probably the source of your error. memory doesn't have any value yet, so when you try to access the function "createMemory" inside memory, you get a null pointer error.
In general, try to give more information when you ask a question. This question would be much clearer if you added what error you were getting, what line it occurs at, the error message, or similar things.
Upvotes: 0
Reputation: 409266
You have the member LinkedList::head
which you initialize to a null pointer.
Then in LinkedList::createMemory
you do node* temp=head
which makes temp
a null pointer.
Lastly in the loop in createMemory
you do temp->name="Free"
which dereferences the null pointer and lead to undefined behavior and a very likely crash.
If you want 32 preallocated nodes in the list, then you should actually allocate memory for those nodes.
Upvotes: 1