【问题标题】:Class Stack Memory Problems (Killed error)类堆栈内存问题(Killed 错误)
【发布时间】:2015-06-29 10:13:14
【问题描述】:

我使用堆栈类制作了一个 Post-Fix(RPN) 计算器。在一些帮助下,我设法摆脱了编译器错误,但现在我对内存一无所知。执行程序时,在输入过程中会出现终止的错误消息。我也无法摆脱输入。我已经检查了三巨头,我认为它们很好。

主要

#include <iostream>
#include <cstdlib>
#include <string>
#include "stack.h"
#include "abstractstack.h"
#include "robotcalc.h"
#include <sstream>
using namespace std;

int main()
{
    string userInput;
    Stack<int> operandStack;


    do
    {
        int number;

        cin >> userInput;

        if (istringstream(userInput) >> number)
        {
            operandStack.push(number);
        }
        else if (isOperator)
        {
            performOperation(userInput, operandStack);
        }
    } while (userInput != "@");

}

主要程序实现: “#”运算符打印堆栈

$ 操作符清栈

@表示输入结束

 #include<string>
 #include<cstdlib>
 #include<iostream>
 #include"robotcalc.h"
 #include "stack.h"

using namespace std;

bool isOperator(const string input)
{
    const int NUM_OPERATORS = 10;

    string operatorArray[] = { "+", "-", "*", "/", "%", "!", "SUM", "R", "#", "R" };

    for (int i = 0; i < NUM_OPERATORS; i++)
    {
        if (input == operatorArray[i])
            return true;
        else
            return false;
    }
}

void performOperation(const string& input, Stack<int> operandStack)
{
    int result;
    int leftOperand, rightOperand;


    if (input == "!")
    {
        result = operandStack.top() * -1;
        operandStack.pop();
        operandStack.push(result);

    }

    if (input == "+")
    {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand + rightOperand;
        operandStack.push(result);
    }

    if (input == "-")
    {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand - rightOperand;
        operandStack.push(result);
    }

    if (input == "*")
    {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand * rightOperand;
        operandStack.push(result);
    }

     if (input == "/")
     {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand / rightOperand;
        operandStack.push(result);
    }

     if (input == "%")
    {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand - rightOperand;
        operandStack.push(result);
    }

    if (input == "#")
    {
        cout << "[";
        while (operandStack.getTop() != NULL)
        {
           cout << operandStack.top() << ",";
           operandStack.pop();
        }
        cout << "]" << endl;
    }

    if (input == "SUM")
    {
        while (operandStack.getTop() != NULL)
        {
            result += operandStack.top();
            operandStack.pop();
        }
        operandStack.push(result);
    }

    if (input == "R")
    {
        operandStack.reverse();
    }

}

堆栈类实现:

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

template class Stack<int>;

template<class T>
const Stack<T>& Stack<T>::operator = (const Stack<T>& rhs)
{
    if (this != &rhs)
    {
        if (Top != NULL)
        {
            clear();
        }
        Top = NULL;
        Node<T>* rhsPtr = rhs.Top;
        Node<T>* copyPtr = Top = new Node<T>;
        copyPtr->Data = rhsPtr->Data;
        while (rhsPtr->next != NULL)
        {
            rhsPtr = rhsPtr->next;
            copyPtr->next = new Node<T>;
            copyPtr = copyPtr->next;
            copyPtr->Data = rhsPtr->Data;
        }
        copyPtr->next = NULL;
    }
    return(*this);
}

template<class T>
Stack<T>::Stack(const Stack<T>& rhs)
{
    Top = NULL;
    *this = rhs;
}

template<class T>
bool Stack<T>::isEmpty() const
{
     return(Top == NULL);
}

template<class T>
const T& Stack<T>::top() const throw(Oops)
{
    if (Top != NULL)
        return(Top->Data);
    else
        throw Oops("Stack is Empty!");

}

template<class T>
void Stack<T>::push(const T& x)
{
    Node<T>* newNode = new Node<T>;
    newNode->Data = x;

    if (isEmpty())
     {
      Top = newNode;
      return;
    }

    newNode->next = Top;
    Top->next = newNode;

 }

 template<class T>
 void Stack<T>::pop()
 {
    Node<T>* temp;
    if (Top != NULL)
    {
        temp = Top;
        Top = Top->next;
        delete temp;
    }
 }

template<class T>
void Stack<T>::clear()
 {
    Node<T>* temp;
    temp = Top;
    while (temp != NULL)
    {
        Top = Top->next;
        delete temp;
        temp = Top;
    }
}

template<class T>
void Stack<T>::reverse()
{
    Node<T>* current;
    Node<T>* previous;
    Node<T>* next;

    previous = NULL;
    current = Top;
    while (current != NULL)
    {
        next = current->next;
        current->next = previous;
        previous = current;
        current = next;
    }

    Top = previous;
}

堆栈类头:

#ifndef STACK_H
#define STACK_H

#include<string>
#include<iostream>
#include<cstdlib>
#include "abstractstack.h"
using namespace std;



template<typename T>
struct Node
{
    T Data;
    Node<T>* next;
};


template<typename T> 
class Stack : public AbstractStack<T>
{
private:
    Node<T>* Top;

public:

    //Purpose: Destructor
    //Postconditions: The stack is now empty
    ~Stack() {}

    //Purpose: Default Constructor
    //Postconditions: Top is initialized to 'NULL'
    Stack(): Top(NULL) {}


    //Copy Constructor
    Stack(const Stack<T>& rhs);

    //Overloaded = Operator
    //Postconditions: *this is now equal to rhs
    const Stack<T>& operator = (const Stack<T>& rhs);


    //Purpose: Check if a stack is empty
    //Returns: 'true' if the stakc is empty, 'false' otherwise
    bool isEmpty() const;

    //Purpose: Looks at the top of the stack
    //Returns: a const reference to the element currently on top of the stack
    //Exception: if the stack is empty, THROW a 'Oops' object with an error message!!!"
    const T& top() const throw(Oops);

    //Purpose: push an element into the stack
    //Parameters: x is the value to push into the stack
    //Postconditions: x is now the element at the top of the stack
    void push(const T& x);

    //Purpose: pop the stack
    //Postconditions: the element formerly at the top of the stack has been removed
    //Popping an empty stack results in an empty stack
    void pop();

    //Purpose: clears the stack
    //Postconditions: the stack is now empty
    void clear();

    //Reverses the stack
    //Postconditions: stack is now in reverse order
    void reverse();


    //Getter Function for Top member variable
    Node<T>* getTop() const { return(Top); }
};

#include "stack.hpp"
#endif

Stack 类是来自纯虚拟调用“AbstractStack”的派生类,我懒得发帖了。

示例输入

1 2 3 # $

4 3 * ! #

20 3 / # $ #

62 #$

2 3 8 * + #

4 48 4 2 + / #

总和#$

1 2 3 4 5 #

R#

@

样本输出

[3,2,1]

[-12]

[ 6, -12 ]

[ ]

[62]

[26]

[ 8, 4, 26 ]

[38]

[5,4,3,2,1]

[ 1, 2, 3, 4, 5 ]

任何想法或帮助将不胜感激。

【问题讨论】:

  • 为了演示问题,输入肯定是没有必要的。同样,堆栈是一个模板并且它是从另一个基类派生的这一事实。此外,这应该都适合一个文件。因此,您现在的任务是将所有这些简化为一个最小但完整的示例。您可能会在途中发现错误。

标签: c++ memory-leaks stack postfix-notation


【解决方案1】:

家庭作业

什么应该捕获异常?我怀疑您遇到的是未捕获的抛出异常。 尝试添加一个 try/catch 来处理它。另外,您是否尝试使用调试器运行? 我在浏览您的代码时注意到的另一件事是,您在实现 operator %. 祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2019-10-16
    • 2015-04-11
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多