为什么这些c ++代码段中存在内存错误?

时间:2020-03-19 15:29:26

标签: c++

目的:我正在编写一个将表达式字符串存储在链接列表中的函数。例如,给定字符串"9+1"。执行该函数后,将创建一个链接列表,其节点存储浮点数9 ,字符'+'和浮点数1(每个节点存储一个字符或浮点数)。 这是代码:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

typedef struct EXPnode {       // Here define the structure of the linklist
    float operand;            // store the operand
    char operation;           // store the operation
    bool judge;               //  judge if the node stores operand or operation
    struct EXPnode *next;     //   point to next node
} EXPnode,*LinkEXP;


void ListOutput(LinkEXP &L)                  //here will output each node of the linklist
{
    EXPnode* p;
    p = L->next;
    cout<<"here should be 9"<<p->operand<<endl;
    p=p->next;
    cout<<"here should be +"<<p->operation<<endl;
    p=p->next;
    cout<<"here should be 1"<<p->operand<<endl;
}

bool isOprAll(char ch)              // here will judge the type
{
    if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^' || ch == '%' || ch == '(' || ch == ')')
        return true;          
    return false;            
}

void CreateArray(EXPnode *arraya[])     // here will create an array which each element stores a linklist
{
  for(int i=0;i<10;i++)
  {
      EXPnode *L = new EXPnode;
      L->next = NULL;
      arraya[i] = L;
  }
}

void strTolist(string str,LinkEXP &E)      // here is the most important function
{
    LinkEXP r = E;
    string Stroper;               // Stroper will store the operand(string type)
    float  Floper;            // Floper will store the operand(float type)
    int i = 0;
    while(i<str.length())
    {
        if(!isOprAll(str[i]))     
        {
            Stroper = Stroper + str[i];       
        }
        else                        
        {
            if(Stroper.length()!=0)   
            {
                Floper = atof(Stroper.c_str());    // turn the string type operand to float type operand
                r->next = new EXPnode;         // add the operand to the node
                r = r->next;
                r->next = NULL;
                r->operand = Floper;        
                r->judge = false;         
            }
            Stroper = "";   
            r->next = new EXPnode;      //  // add the operation to the node
            r = r->next;
            r->next = NULL;
            r->operation = str[i];      
            r->judge = true;            
        }
        i++;
    }
}



int main()
{
    EXPnode *arrayList[10];
    CreateArray(arrayList);
    strTolist("9+1",arrayList[0]);
    ListOutput(arrayList[0]);
    return 0;
}

Eyerything看起来不错。我认为它最终将输出1+9。但是我看到的是:

enter image description here

否,1在哪里?看起来像是内存错误。为什么?代码有什么问题吗?我检查了很长时间却没有找到错误。

2 个答案:

答案 0 :(得分:2)

到达字符串的末尾时,您会处理Stroper中存储的部分字符串,而不会对其进行处理,因此永远不会为丢失的1创建节点。

答案 1 :(得分:0)

抛出异常:读取访问冲突。 p->下一个是nullptr。

void ListOutput(LinkEXP& L)                  //here will output each node of the 
 linklist
{
    EXPnode* p;
    p = L->next;
    cout << "here should be 9" << p->operand << endl;
    p = p->next;
    cout << "here should be +" << p->operation << endl;
    p = p->next; // **<- HERE**
    cout << "here should be 1" << p->operand << endl;
}