簡體   English   中英

我的堆棧實現(C ++)代碼中出現段錯誤,但無法理解原因

[英]Segfault in my stack implementation (C++) code, but cannot understand why

為使記錄更清晰,我對C ++相當陌生,我在編程方面的大部分經驗是Java。 我正在寫一個基於數組的堆棧實現,但似乎無法讀取保存在堆棧中的任何數據。

可以推入對象,這似乎可以正常運行。 注釋掉top()操作會產生成功的零輸出,這似乎告訴我,我知道該項目至少已添加到數組中。

但是,閱讀該項目時,出現了段錯誤。 經過一兩個快速的Google搜索,我了解到segfault意味着有一個操作正在訪問它無權訪問的數據。 這使我認為我的top()函數無法訪問該數組。 它是類的私有成員,但是我以前對OOP的了解告訴我,類方法應該可以訪問所有私有變量。

有人可以幫我指出正確的方向嗎? (很抱歉,如果文檔對於這種原始數據結構有點過多,請告知是否應刪除它)。

謝謝! 代碼如下:

#include <iostream>
using namespace std;

/*
Class: arrayStack() 
A simple stack implemented with a single array. 
Handles char objects.
*/
class arrayStack {

    int length; //cap on stack length
    int count; //Keeps track of which element in the array to pop from.
    char array[]; //Hold data here

public: 

/*
arrayStack() 
Constructor used to create a stack object. No default constructor exists, the length (l) is a required argument as a parameter to create a stack.
l space is reserved in the array and count is set to -1, the int value required for an empty stack.
*/
arrayStack(int l) 
{
    char array[l];
    length = l;
    count = -1;
}

/*
push() -- return type void.
Method to add a desired char element (o) to the stack. 
*/
void push(char o) 
{   
    array[count] = o;
    count++; //Increment the counter to accurately pull element from array.
}

/*
pop() -- return type char.
Method to remove an element from the stack. Element is pulled from the stack and returned. 
*/
char pop() 
{
    //temp space to store pulled element before returning.
    char temp;
    temp = array[count];

    //Replace popped element with null to reduce memory waste. Then decrement counter for proper functioning of the stack.
    array[count] = '\0'; 
    count--;
    return temp;
}

/*
top() -- return type char.
Method which returns the top element of the function without deleting it from the stack. Useful for inspection and testing, but functionally very similar to pop().
*/
char top() 
{
    //temp space to store pulled element.
    char temp; 
    temp = array[count];
    return temp;
}

/*
isEmpty() -- return type boolean.
Method which checks the stack for elements. If there are no elements, the method returns true. If there exists one or more elements, the method will return false. 
Method is very simple-- a simple check against the count (int) variable. 
*/
bool isEmpty() 
{
    if (count == -1) {
        return true;
    } else {
        return false;
    }
}

/*
size() -- return type int.
Method which returns the number of elements in the stack. 
*/
int size() 
{
    return count++;
}

};

int main() {
arrayStack stack(10);
stack.push('c');
cout << stack.top(); //SEGFAULT OCCURS HERE. 
cout << stack.isEmpty();

return 0;
}

您的成員array仍未初始化:

arrayStack(int l) 
{
    char array[l];
    length = l;
    count = -1;
}

在這里,您將創建一個新的array (我仍然懷疑這是合法的,因為C ++不支持VLA。它應該是

arrayStack(int l) 
{
    array = new char[l];
    length = l;
    count = -1;
}

您還需要實現一個析構函數以刪除分配的內存。 這意味着您還需要一個復制構造函數和一個復制賦值運算符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM