0

To set the record straight, I am fairly new to C++ and most of my experience in programming is in Java. I'm writing an array based stack implementation and I cannot seem to read any of the data saved in my stack.

Objects can be pushed and this seems to function normally. Commenting out the top() operation yields a successful output of zero, which seems to tell me that I know the item is at least being added to the array.

However, reading the item, I get a segfault error. After a quick google or two, I learned that a segfault means that there is an operation accessing data it does not have access to. This leads me to think that my top() function does not have access to the array. It is a private member of the class, but my previous knowledge of OOP tells me that a class method should have access to all private variables.

Could anyone help point me in the right direction here? (Apologies if the documentation is a bit excessive for such a primitive data structure, let me know if it should be removed).

Thanks! Code is below:

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

1 回答 1

4

您的成员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;
}

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

于 2012-07-03T07:29:33.163 回答