【问题标题】:getting a run time error in dynamic memory allocation在动态内存分配中出现运行时错误
【发布时间】:2017-08-08 12:49:48
【问题描述】:

程序应该像操作系统那样分配内存。

这是主要功能

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

这是链表类及其功能

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

每当我调用其中一个类函数时,我都会遇到运行时错误,我不明白为什么

【问题讨论】:

    标签: c++ linked-list runtime-error dynamic-memory-allocation


    【解决方案1】:

    您有成员 LinkedList::head,您将其初始化为空指针。

    然后在 LinkedList::createMemory 中执行 node* temp=head,这使 temp 成为空指针。

    最后在createMemory 的循环中,您执行temp-&gt;name="Free",这会取消引用空指针并导致未定义的行为 并且很可能会崩溃。

    如果您希望列表中有 32 个预先分配的节点,那么您实际上应该为这些节点分配内存。

    【讨论】:

    • @Matteo head = temp = new node 怎么样?
    • 所以将 temp 声明为指向节点的指针,然后写入 head = temp =new node
    【解决方案2】:
    LinkedList memory;
    memory.createMemory();
    

    此代码可能是您的错误的根源。内存还没有任何值,所以当你尝试访问内存中的函数“createMemory”时,你会得到一个空指针错误。
    一般来说,当你提出问题时,尽量提供更多信息。如果您添加您遇到的错误、发生在哪一行、错误消息或类似内容,这个问题会更清楚。

    【讨论】:

    • 它没有给我一个具体的错误它只是说 program2.exe 停止工作然后它崩溃了
    • 这很不寻常。你用什么来执行代码?
    • 我希望它会打印错误消息,但我对代码块没有任何经验,所以我会听从知识渊博的人。
    【解决方案3】:

    当你调用Memory.createMemory(); 时,这个程序会崩溃。 因为你没有在函数createMemory()中为temp分配内存,所以temp指向NULL,所以temp-&gt;name="Free";会让你的程序崩溃。您应该使用newtemp 分配内存。您应该尝试this site 来帮助您。

    BTW,你对待命令参数的方式不合适,如果用户输入了2个以上的参数,你告诉用户有Not enough commands

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

    【讨论】:

    • 这是分配节点* temp的正确方法吗? temp = 新节点;
    • 是的,没错。但我认为您确实需要查看this site 以了解有关动态内存的更多信息@Matteo
    • 我阅读了该站点,然后运行了调试器,但我仍然不明白为什么它给出的错误是 temp->name="Free"
    • 因为 temp 指向 NULL,它根本不代表什么。当你从无中问出诸如字符串“Free”之类的东西时,程序会崩溃。@Matteo
    猜你喜欢
    • 1970-01-01
    • 2017-06-06
    • 2021-10-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多