简体   繁体   中英

Why there is a memory error in these c++ code snippets?

Purpose:I am writing a function to store an expression string in a linklist.For example,given string "9+1" .After executing the function,there is a linklist created whose nodes store float number 9 ,char '+' and float number 1 (each node stores one char or float number). Here are the codes:

#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 looks fine.And I have thought it will output 1 , + , 9 at last.But what i saw is:

在此处输入图片说明

No,where is the 1 ? Looks like a memory error.But why?Is there anything wrong with the codes?I check for a long time yet didn't find the error.

当您到达字符串的Stroper ,您不会处理存储在Stroper中的部分字符串,因此您永远不会为丢失的1创建节点。

Exception thrown: read access violation. p->next was 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM