对象初始化后在C++中显示 char 数组时的异常行为



main((:

char inp[] = "(A+B)/(C*D))";
Infix i;
cout << "In main: " << inp  /* + ')' */  << endl << endl;

下面是Infix构造函数:

Infix() {
push('(');
cout << "Element In Stack: " << *returnElement(returnTop()) << endl;
outputString = "";
strcpy(operatorArray, "/*-+%");
cout << "Operator Array: " << operatorArray << endl;
}

中缀继承自类"堆栈":

class Stack{
int top = -1;
char arr[100];
public:
bool push(char);
char pop();
char peek();
bool isEmpty();
void displayAll();
char returnTop() { return top;}
char* returnElement(int i) {
if(i > 98){
cout << "StackOutOfIndex";
return nullptr;
}
return &arr[i];
}

};

当我在 main 中运行代码时,它会显示异常输出:

Element In Stack: (
Operator Array: /*-+%
In main: +%

Stack Object Destroyed!

但是,在 main 中,如果注释声明"中缀"对象声明的行,则代码运行良好:

In main: (A+B)/(C*D))

编辑:

堆栈类

#include<iostream>
using namespace std;
class Stack{
int top = -1;
char arr[100];
public:
bool push(char);
char pop();
char peek();
bool isEmpty();
void displayAll();
char returnTop() { return top;}
char* returnElement(int i) {
if(i > 98){
cout << "StackOutOfIndex";
return nullptr;
}
return &arr[i];
}
};
bool Stack:: push(char elementToPush) {
if(top > 98) {
cout << "nStack Overflow!!";
return false;
} else {
arr[++top] = elementToPush;
return true;
}
}
char Stack:: pop() {
if(top <= -1) {
cout << "nStack Underflow!!";
return ' ';
} else {
return (arr[top--]);
}
}
char Stack:: peek() {
if(top > 98) {
cout << "nStack Overflow!!";
return ' ';
} else {
return arr[top];
}
}
bool Stack:: isEmpty() {
return (top <= 0);
}
void Stack:: displayAll() {
if(top <= -1) {
cout << "null";
return;
}
int i = top;
while (i >= 0) {
cout << arr[i] << " ";
--i;
}
cout << "n";
}

中缀类

#include<iostream>
#include<cstring>
#include<D:Programs11Stack.cpp>
using namespace std;
class Infix : public Stack {
string outputString;
char operatorArray[];
public:
Infix() {
push('(');
cout << "Element In Stack: " << *returnElement(returnTop()) << endl;
outputString = "";
strcpy(operatorArray, "/*-+%");
cout << "Operator Array: " << operatorArray << endl;
}
string infixToPostfix(char *, int);
bool manupulateOperator(char, int); 
int checkPrecedence(char);
~Infix() {
cout << "nStack Object Destroyed!" << endl;
}
};
string Infix:: infixToPostfix(char *str, int size) {
cout << "nGiven String: " << str << endl;
int x;
for(int i = 0; i < size; ++size) {
x = str[i];
if(x != ' ') {
if(x == ')') {
while(returnTop() != '(') {
cout << pop() << " popped!n";
}
cout << pop() << " popped!n";
} else if(isalpha(x)) {
cout << x;
} /* else{ // scanned character is an operator
if(manupulateOperator(x, i)) {
} else {
return " ";
}
} */
}
}
return outputString;
}
bool Infix::manupulateOperator(char c, int position) {
try {
char topElement = *returnElement(returnTop());
if(checkPrecedence(c) == -1) {
cout << "nErrn";
}else if((checkPrecedence(c) > checkPrecedence(topElement)) || returnTop() == 0) {
push(c);
cout << c << " pushed!n";
}
} catch(std::exception e) {
std::cerr << e.what() << 'n';
return false;
} catch (char* Ce) {
cout << Ce << endl;
}
return true;
}
int Infix::checkPrecedence(char c) {
/* 
+ -> 1
- -> 1
* -> 2
/ -> 2
% -> 2
*/
switch(c) {
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
case '%':
return 2;
default:
// throw "Illegal Operator Detected!";
cout << "Illegal Operator Detected: " << c << endl;
return -1;
}
}
int main() {
cout << endl;
int x = 1;
char inp[] = "(A+B)/(C*D))";
//Infix i;
cout << "In main: " << inp  /* + ')' */  << endl << endl;
// cout << i.infixToPostfix(input + ')', sizeof(input));
/*     for(int i = 0; i < strlen(inp); ++i) {
cout << inp[i];
}
*/
return 0;
}

您正在将operatorArray声明为char数组,但您没有为其分配任何内存!因此,当您在Infix构造函数中调用strcpy(operatorArray, "/*-+%");时,您将尝试将给定的字符串常量复制到尚未分配的内存中,从而导致未定义的行为- 这似乎覆盖了main中声明的inp[]数组。

为了解决这个问题,我建议给你的operatorArray成员一个特定的大小,这个大小将足够大,以容纳你想要复制到它的任何字符串 - 8个字符将在你给出的示例代码中工作:

class Infix : public Stack {
string outputString;
char operatorArray[8]; // Make this member a REAL array of characters.
//..

调用构造函数时,变量char operatorArray[]没有分配内存。当您使用strcpy时,您会写入内存中没有权限的位置,因此写入其他信息。
要找到这些类型的错误,我建议使用valgrind。

标准C++中不允许使用char operatorArray[];

如果您没有看到错误消息,那么我建议您调整编译器设置以遵循该语言的标准形式,这将为您节省大量时间。

最新更新