0

これは、C++での私の最初の哀れな試みです。C ++で配列ベースのスタックを実行しましたが、デストラクタがメモリダンプをスローしています。何が悪かったのかわかりません。



#include <stdio.h>
#include <iostream>
#include <exception>

using namespace std;

class FullStackException : public exception {
    virtual const char* what() const throw() {
        return "Stack is full.";
    }
} fsex;

class EmptyStackException : public exception {
    virtual const char* what() const throw() {
        return "Stack is empty.";
    }
} esex;

template <class D>
class ArrayBasedStack {
private:
    int t; //t represents top
    D *S;
    int arrSize;
public:
    ArrayBasedStack(int arraySize = 10);
    ~ArrayBasedStack();
    int size(); /*returns the number of elements stored*/
    void push(D&); /*inserts an element*/
    D pop(); /*removes and returns the last inserted element*/
    D top(); /*returns the last inserted element without removing it*/
    int isEmpty(); /*indicates whether no elements are stored*/
};

template <class D>
ArrayBasedStack<D>::ArrayBasedStack(int arraySize) {
    /* Elements are added from left to right */
    S = new D[arraySize];
    arrSize = arraySize;
    /* t keeps track of the index of the top element */
    t = -1;
}

template <class D>
ArrayBasedStack<D>::~ArrayBasedStack() {
    if(S != NULL) {
        int i = 0;
        for(i = 0; i < size(); i++) {
            S[i] = NULL;
        }
        cout << "about to delete S" << endl;
        delete[] S;
    }
}

template <class D>
int ArrayBasedStack<D>::size() {
    return t;
}

template <class D>
void ArrayBasedStack<D>::push(D& data) {
    if(t == arrSize) {
        throw fsex;
    } else {
        S[t] = data;
        t++;
    }
}

template <class D>
D ArrayBasedStack<D>::pop() {
    if(isEmpty()) {
        throw esex;
    }
    D element = S[t];
    S[t--] = NULL;
    return element;
}

/*
 * returns true if the stack is empty, false otherwise
 */
template <class D>
int ArrayBasedStack<D>::isEmpty() {
    return (t < 0);
}

int main(int argc, char *argv[]) {

    char inputs[][10] = {
        "str1"
    };

    char *i = NULL;

    ArrayBasedStack<char *> stack;
    i = inputs[0];
    stack.push(i);
    try {
        stack.pop();
    }
    catch(exception& ex) {
        cout << "ERR:" << ex.what() << endl;
    }
    return 0;
}


4

2 に答える 2

2

問題の行は

    t = -1;

する必要があります

    t = 0;

最初の要素を追加すると、次のコードが実行されるためです

    } else {
       S[t] = data;    // t == -1
       t++;
    }
于 2010-09-01T02:08:34.160 に答える
0

以下が原因です。

template <class D> 
void ArrayBasedStack<D>::push(D& data) { 
    if(t == arrSize) { 
        throw fsex; 
    } else { 
        S[t] = data;       // Should be S[++t] = data;
        t++;               // Comment out this line
    } 
} 

この実装では、「t」が次の使用可能な場所ではなく、スタックの最上位の要素を指していることを前提としています。push

演算子[]と演算子++の優先順位は同じであることに注意してください。それらは左から右に関連付けられるため、[]は演算子++の前に評価されます。

あなたの実装では、ここに問題があります。tが-1に初期化されると、S[-1]にある配列の添え字を超えて上書きされます。これは。になりundefined behaviorます。

少なくとも私のシステムでは、スタッククラスのデストラクタのメモリを解放しようとしているときに問題が発生します。これは、グーフアップが発生した後もずっと目に見える症状の典型的な例です。

pushまた、パラメータを次のように取得することをお勧めしますD const &

于 2010-09-01T02:35:45.437 に答える