2017-03-17 50 views
1

该程序应该按照操作系统分配内存。在动态内存分配中获取运行时错误

这是主要的功能

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++; 
} 
}; 

每当我调用的类函数,我得到一个运行时错误之一,我不不知道为什么

回答

1

你有成员LinkedList::head你初始化为空指针。

然后在LinkedList::createMemory你做node* temp=head这使得temp一个空指针。

最后在createMemory环路你做temp->name="Free"其解引用空指针,导致未定义行为和极有可能崩溃。

如果你想在列表中有32个预分配节点,那么实际上你应该为这些节点分配内存。

+0

那么我怎样才能初始化温度非空指针? – Matteo

+0

@Matteo'head = temp = new node'如何? –

+0

所以声明临时指针节点,然后写头=临时=新节点 – Matteo

0
LinkedList memory; 
memory.createMemory(); 

此代码可能是您的错误的来源。内存没有任何价值,所以当你尝试访问内存中的函数“createMemory”时,你会得到一个空指针错误。
通常,当您提出问题时,请尝试提供更多信息。如果您添加了您收到的错误,错误消息或类似事情发生的位置,此问题将更加清晰。

+0

它并没有给我一个具体的错误,它只是说program2.exe停止工作,然后崩溃 – Matteo

+0

这是不寻常的。你用什么来执行代码? –

+0

我在codeblocks中使用GNU GCC编译器 – Matteo

0

当您调用Memory.createMemory();时,该程序将崩溃。 由于您没有在函数createMemory()中为temp分配内存,因此temp指向NULL,因此temp->name="Free";会使程序崩溃。您应该使用newtemp分配内存。你应该尝试this site来帮助你。

顺便说一句,你对待命令参数的方式是不合适的,如果用户输入2个以上的参数,并且你告诉用户有Not enough commands

if(argc==2) 
    command = argv[1]; 
else cout<<"Not enough commands"<<endl; 
+0

这将是一个正确的方式来分配它的节点* temp; temp =新节点; – Matteo

+0

是的,它是正确的。但我认为你确实需要检查[本站](http://www.cplusplus。com/doc/tutorial/dynamic /)了解更多关于动态内存的信息@Matteo – Jiahao

+0

我读了网站,然后我运行了调试器,但是我仍然不明白为什么它给出的错误是temp-> name =“Free” – Matteo