- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的程序执行得很好,除了最后,当我调试它时,它在调用我的析构函数后出现段错误。我不确定问题的原因是什么。我已经发布了 2 个相关文件。
Breakpoint 1, main () at Prog3.cc:12
12 cout << "Program executed" << endl;
(gdb) s
Program executed
~Lex (this=0x80375c4) at lex.cc:36
36 delete [] str;
(gdb) s
37 }
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0xfef49418 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string () from /usr/sfw/lib/libstdc++.so.6
(gdb) Quit
lex.h如下:
#ifndef LEX_H
#define LEX_H
#include "token.h"
#include <iostream>
#include <stdlib.h>
class Lex {
public:
Lex(istream& in, string fileName);
//Lex();
//Lex(const Lex& l);
~Lex();
static const int INTLIT = 1;
static const int FLOATLIT = 2;
static const int STRLIT = 3;
static const int IDENT = 4;
static const int PLUS = 5;
static const int MINUS = 6;
static const int TIMES = 7;
static const int DIVIDE = 8;
static const int REM = 9;
static const int ASSIGN = 10;
static const int LPAREN = 11;
static const int RPAREN = 12;
static const int COMMA = 13;
static const int EQ = 14;
static const int LT = 15;
static const int LE = 16;
static const int GT = 17;
static const int GE = 18;
static const int NE = 19;
static const int SET = 20;
static const int PRINT = 21;
static const int WHILE = 22;
static const int DO = 23;
static const int END = 24;
static const int AND = 25;
static const int OR = 26;
static const int IF = 27;
static const int THEN = 28;
static const int ELSE = 29;
static const int ENDIF = 30;
static const int PROGRAM = 31;
static const int ENDOFFILE = 32;
static const int ERROR = 33;
int charIndex;
int lineIndex;
int spaceIndex;
int lineNum;
int lineLength[100];
char ch;
bool hashCheck;
bool stopLoop;
Token nextToken();
char nextChar();
char str[256][256];
bool checkSet;
void printLex();
string idents[256];
int identCount;
friend ostream& operator<<(ostream& out, const Token& t);
};
#endif
#include "lex.h"
#include "token.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
lex.cc 在这里。
Lex::Lex(istream& in, string fileName) {
stopLoop = false;
charIndex = 0;
lineIndex = 0;
identCount = 0;
lineNum = 0;
hashCheck = false;
checkSet = false;
int tempSize;
ifstream file;
string temp;
for (int i = 0; i < 100; ++i)
lineLength[i] = 0;
if (!file.is_open()) { file.open(fileName.c_str()); }
while(!file.eof()) {
std::getline(file,temp);
tempSize = temp.size();
for (int i = 0; i < tempSize; ++i) {
str[lineNum][i] = temp[i];
lineLength[lineNum] += 1;
}
lineNum++;
}
file.close();
}
Lex::~Lex() {
delete [] str;
}
void Lex::printLex() {
charIndex = 0;
lineIndex = 0;
while (stopLoop == false) {
cout << nextToken();
// cout << "Line index: " << lineIndex << endl;
}
}
ostream& operator <<(ostream& out, const Token& t) {
out << t.type() << " \t " << t.lexeme() << " \t \t " << (t.line() + 1) << " \t \t " << t.pos() << endl;
return out;
}
bool isReal(char ch) {
string alphabet = "abcdefghijklmnopqrstuvwxyz1234567890(){}<>+-/=!*,%&|.";
if (alphabet.find(ch) != alphabet.npos) return true;
else return false;
}
bool isNum(char ch) {
string specialChars = "1234567890.";
if (specialChars.find(ch) != specialChars.npos) return true;
else return false;
}
bool isNumFinal(string b) {
int length = b.length();
const char* temp = b.c_str();
bool henry = true;
for (int i = 0; i < length; ++i) {
if (henry == false) { break; }
henry = isNum(temp[i]);
}
return henry;
}
bool isSpecialChar(char ch) {
string specialChars = "(){}<>+-/=!*,%&|";
if (specialChars.find(ch) != specialChars.npos) return true;
else return false;
}
char Lex::nextChar() {
if (lineIndex >= lineNum) {
// cout << "End of file reached\n";
stopLoop = true;
return '#';
}
else if (charIndex >= lineLength[lineIndex]) {
lineIndex++;
charIndex = 0;
return nextChar();
}
else if (str[lineIndex][charIndex] == '#') {
hashCheck = true;
while (hashCheck = true) {
if (str[lineIndex][charIndex] == '#') { hashCheck = false; }
charIndex++;
if (charIndex > lineLength[lineIndex]) {
charIndex = 0;
lineIndex++;
}
}
}
else {
ch = str[lineIndex][charIndex];
charIndex++;
return ch;
}
cout << "you shouldn't be here\n";
return str[lineIndex][charIndex];
}
Token Lex::nextToken() {
if (charIndex == lineIndex && charIndex == 0) { ch = nextChar(); }
while (ch == ' ') { ch = nextChar(); }
// cout << "CI: " << charIndex << endl;
string build;
int tempCharIndex = charIndex;
int tempLineIndex = lineIndex;
build += ch;
if (charIndex == lineIndex && charIndex == 0) { build = ""; }
if (checkSet == true) {
checkSet = false;
while (ch != ' ' && tempLineIndex == lineIndex) {
ch = nextChar();
if (ch != ' ' && tempLineIndex == lineIndex)
build += ch;
}
idents[identCount] = build;
identCount++;
return Token(IDENT, build, tempLineIndex, tempCharIndex);
}
else if (isSpecialChar(ch)) {
switch(ch) {
case '(':
ch = nextChar();
return Token(LPAREN, build, tempLineIndex, tempCharIndex);
case ')':
ch = nextChar();
return Token(RPAREN, build, tempLineIndex, tempCharIndex);
case '{':
ch = nextChar();
return Token(THEN, build, tempLineIndex, tempCharIndex);
case '}':
ch = nextChar();
return Token(ENDIF, build, tempLineIndex, tempCharIndex);
case '+':
ch = nextChar();
return Token(PLUS, build, tempLineIndex, tempCharIndex);
case '-':
ch = nextChar();
return Token(MINUS, build, tempLineIndex, tempCharIndex);
case '/':
ch = nextChar();
return Token(DIVIDE, build, tempLineIndex, tempCharIndex);
case '*':
ch = nextChar();
return Token(TIMES, build, tempLineIndex, tempCharIndex);
case '=':
ch = nextChar();
if (ch == '=') {
build += ch;
ch = nextChar();
return Token(EQ, build, tempLineIndex, tempCharIndex);
}
else {
return Token(ASSIGN, build, tempLineIndex, tempCharIndex);
}
case '>':
ch = nextChar();
if (ch == '=') {
build += ch;
ch = nextChar();
return Token(GE, build, tempLineIndex, tempCharIndex);
}
else return Token(GT, build, tempLineIndex, tempCharIndex);
case '<':
ch = nextChar();
if (ch == '=') {
build += ch;
ch = nextChar();
return Token(LE, build, tempLineIndex, tempCharIndex);
}
else return Token(LT, build, tempLineIndex, tempCharIndex);
case '!':
ch = nextChar();
if (ch == '=') {
build += ch;
ch = nextChar();
return Token(NE, build, tempLineIndex, tempCharIndex);
}
else return Token(ERROR, build, tempLineIndex, tempCharIndex);
case '%':
ch = nextChar();
return Token(REM, build, tempLineIndex, tempCharIndex);
case '&':
ch = nextChar();
return Token(AND, build, tempLineIndex, tempCharIndex);
case '|':
ch = nextChar();
return Token(OR, build, tempLineIndex, tempCharIndex);
default:
return Token(ERROR, build, tempLineIndex, tempCharIndex);
}
}
else if (isNum(ch)) {
tempCharIndex = charIndex;
while (ch != ' ' && tempLineIndex == lineIndex) {
ch = nextChar();
if (isSpecialChar(ch)) {
break;
}
if (ch != ' ' && tempLineIndex == lineIndex)
build += ch;
}
if (isNumFinal(build)) {
if (build.find('.') != build.npos)
return Token(FLOATLIT, build, tempLineIndex, tempCharIndex);
else return Token(INTLIT, build, tempLineIndex, tempCharIndex);
}
else return Token(ERROR, build, tempLineIndex, tempCharIndex);
}
else {
tempCharIndex = charIndex;
while (ch != ' ' && tempLineIndex == lineIndex) {
ch = nextChar();
// cout << "ch: " << ch << endl;
if (ch != ' ' && tempLineIndex == lineIndex)
//cout << "inserted: " << ch << endl;
build += ch;
}
if (build.compare("while") == 0)
return Token(WHILE, build, tempLineIndex, tempCharIndex);
else if (build.compare("if") == 0)
return Token(IF, build, tempLineIndex, tempCharIndex);
else if (build.compare("print") == 0)
return Token(PRINT, build, tempLineIndex, tempCharIndex);
else if (build.compare("end") == 0)
return Token(ENDOFFILE, build, tempLineIndex, tempCharIndex);
else if (build.compare("else") == 0)
return Token(ELSE, build, tempLineIndex, tempCharIndex);
else if (build.compare("do") == 0) { return Token(DO, build, tempLineIndex, tempCharIndex); }
else if (build.compare("set") == 0) {
checkSet = true;
// cout << "CI: " << str[lineIndex] << endl;
return Token(SET, build, tempLineIndex, tempCharIndex);
}
else {
for (int i = 0; i < identCount; ++i) {
if (build.compare(idents[i]) == 0) { return Token(IDENT, build, tempLineIndex, tempCharIndex); }
}
cout << "build:" << build << ".\n";
return Token(STRLIT, build, tempLineIndex, tempCharIndex);
}
}
}
最佳答案
除非您调用了 new[]
,否则不要调用 delete[]
。 str
是您类中的静态大小数组,您既不需要 new[]
也不需要 delete[]
。
您的类完全由将处理自己的清理或不需要清理的对象组成,因此您的析构函数可以简单地为空。甚至不声明它,在这种情况下编译器将为您提供正确的析构函数。
关于c++ - 我的析构函数需要在这个函数中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18021152/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!