c++ - 我的堆栈实现 (C++) 代码中出现段错误,但无法理解原因

标签 c++ stack segmentation-fault

澄清一下,我对 C++ 还很陌生,而且我的大部分编程经验都来自 Java。我正在编写一个基于数组的堆栈实现,但我似乎无法读取堆栈中保存的任何数据。

可以推送对象,这似乎正常运行。注释掉 top() 操作会产生零的成功输出,这似乎告诉我我知道该项目至少被添加到数组中。

但是,在阅读该项目时,我收到了一个段错误。快速谷歌一两次后,我了解到段错误意味着有一个操作访问它无权访问的数据。这让我认为我的 top() 函数无法访问数组。它是类的私有(private)成员,但我以前的 OOP 知识告诉我,类方法应该可以访问所有私有(private)变量。

谁能帮我指出正确的方向? (很抱歉,如果文档对于这种原始数据结构来说有点过多,请告诉我是否应该删除它)。

谢谢!代码如下:

#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;
}

在这里,你创建了一个新的数组(我怀疑这是否合法,因为 C++ 不支持 VLA。它可能应该是

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

您还需要实现一个析构函数来删除分配的内存。这意味着您还需要复制构造函数和复制赋值运算符。

关于c++ - 我的堆栈实现 (C++) 代码中出现段错误,但无法理解原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306330/

相关文章:

c++ - 静态初始化顺序失败

c++ - 将未初始化的类实例分配给类指针会导致段错误

performance - Go 语言指针性能

Haskell、Monads、堆栈空间、惰性——如何构造惰性代码?

python - 如何使用检查从绑定(bind)方法中获取类名?

c - 我的段错误

c - 直接返回地址时出现段错误,但通过指针返回地址时没有错误

c++ - 如何在 C++ 中以结构体中的数组为目标

c++ - 确定一个 vector 是否是另一个 vector 的子集的有效方法?

c++ - swap_if_null 的汇编代码