请教大家一个问题
0x00FC7F7D 处有未经处理的异常(在 作业4.15(1).exe 中): 0xC0000005: 写入位置 0xCCCCCCCC 时发生访问冲突。这个问题是什么造成的?
代码如下(一个判断字符串是否为回文的程序)
#include<iostream>
#include<string>
using namespace std;
#define defaultSize 20
void Assert(bool val, string s) {
if (!val) { // Assertion failed -- close the program
cout << "Assertion Failed: " << s << endl;
exit(-1);
}
};
template <typename E> class Stack
{
private:
void operator = (const Stack&){};
Stack(const Stack&){}
public:
Stack(){}
virtual ~Stack(){}
virtual void clear() = 0;
virtual void push(const E& it) = 0;
virtual E pop() = 0;
virtual const E & topValue() const = 0;
virtual int length() const = 0;
};
template<typename E>class Queue
{
private:
void operator = (const Queue&){}
Queue(const Queue&){}
public:
Queue(){}
virtual ~Queue(){}
virtual void clear() = 0;
virtual void enqueue(const E&) = 0;
virtual E dequeue() = 0;
virtual const E&frontValue()const = 0;
virtual int length() const = 0;
};
// Array-based queue implementation
template <typename E> class AQueue : public Queue<E> {
private:
int maxSize; // Maximum size of queue
int front; // Index of front element
int rear; // Index of rear element
E *listArray; // Array holding queue elements
public:
AQueue(int size = defaultSize) { //Constructor
// Make list array one position larger for empty slot
maxSize = size + 1;
rear = 0;
front = 1;
listArray = new E[maxSize];
};
~AQueue() { delete[] listArray; } // Destructor
void clear() { rear = 0; front = 1; } // Reinitialize
void enqueue(const E& it) { // Put "it" in queue
Assert(((rear + 2) % maxSize) != front, "Queue is full");
rear = (rear + 1) % maxSize; // Circular increment
listArray[rear] = it;
}
E dequeue() { // Take element out
Assert(length() != 0, "Queue is empty");
E it = listArray[front];
front = (front + 1) % maxSize; // Circular increment
return it;
}
const E& frontValue() const { // Get front value
Assert(length() != 0, "Queue is empty");
return listArray[front];
}
virtual int length() const // Return length
{
return ((rear + maxSize) - front + 1) % maxSize;
}
};
// Array-based stack implementation
template <typename E> class AStack : public Stack<E> {
private:
int maxSize; // Maximum size of stack
int top; // Index for top element
E *listArray; // Array holding stack elements
public:
AStack(int size = defaultSize) // Constructor
{
maxSize = size; top = 0; listArray = new E[size];
}
~AStack() { delete[] listArray; } // Destructor
void clear() { top = 0; } // Reinitialize
void push(const E& it) { // Put "it" on stack
Assert(top != maxSize, "Stack is full");
listArray[top++] = it;
}
E pop() { // Pop top element
Assert(top != 0, "Stack is empty");
return listArray[--top];
}
const E& topValue() const { // Return top element
Assert(top != 0, "Stack is empty");
return listArray[top - 1];
}
int length() const { return top; } // Return length
};
int main()
{
int n, i;
AStack <char> aStack[20];
AQueue <char> aQueue[20];
char S[20];
cout << "判断一个字符串是否为回文" << endl;
cout << "请输入一串字符(不超过20个元素)" << endl;
cin >> S;
n = sizeof(S[20]);
for (i = 0; i < n; i++)
{
aStack[20].push(S[i]);
aQueue[20].enqueue(S[i]);
}
for (i = 0; i < n; i++)
{
if (aStack[20].topValue() != aQueue[20].frontValue())
{
cout << "false" << endl;
}break;
aQueue[20].dequeue() && aStack[20].pop();
}
}