该程序应该按照操作系统分配内存。在动态内存分配中获取运行时错误
这是主要的功能
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;
}
这是LinkedList类,其功能
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++;
}
};
每当我调用的类函数,我得到一个运行时错误之一,我不不知道为什么
那么我怎样才能初始化温度非空指针? – Matteo
@Matteo'head = temp = new node'如何? –
所以声明临时指针节点,然后写头=临时=新节点 – Matteo