2012-07-03 24 views
0

要直接設置記錄,我對C++來說相當陌生,而且我的大多數編程經驗都是用Java編寫的。我正在寫一個基於數組的堆棧實現,我似乎無法讀取保存在我的堆棧中的任何數據。Segfault在我的堆棧實現(C++)代碼中,但無法理解爲什麼

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

但是,閱讀該項目,我得到一個段錯誤。經過一兩次快速搜索,我發現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; 
} 
+0

在同一個函數中同時使用'l'(ell)和'1'(one)並不會使讀起來更容易。 :-) –

+0

你的大部分邏輯實際上都不能在Java中工作,例如第一個'push'寫入'array [-1]','size()'不應該修改'count'。你真正需要的是一本很好的入門書。 – molbdnilo

回答

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

您還需要實現析構函數來刪除所分配的內存。這意味着你還需要一個拷貝構造函數和拷貝賦值運營商。

+1

'數組'的類型應該是'char *'。 – Naveen

+1

@Naveen Yup,我只是認爲這是一些奇怪的編譯器擴展。 –

+0

@LuchianGrigore我可以問爲什麼數組應該是char *?我不太理解。我無法理解爲什麼我不能在不將整個數組轉換爲指針數組的情況下工作。 – aerotwelve