I've written some code for a scanner in C++, but I keep receiving a segmentation fault. The strange thing is that the segmentation fault happens at the completion of the code. I think it has to do with my scan function and the use of file.get()
in it, but wouldn't that cause a segmentation fault at that line of the code? I have a simple main.cpp that calls the function at does a cout statement and the segmentation fault happens after the return statement. When I run GDB and backtrace I received:
Program received signal SIGSEGV, Segmentation fault.
0x00011260 in __do_global_dtors_aux ()
(gdb)
(gdb) backtrace
#0 0x00011260 in __do_global_dtors_aux ()
#1 0x00012504 in _fini ()
#2 0xfefc3120 in _exithandle () from /lib/libc.so.1
#3 0xfefb10e0 in exit () from /lib/libc.so.1
#4 0x00011218 in _start ()
Here's the simple main.cpp:
int main(int argc, char *argv[]) {
tokenType * token, * testtk;
string filename;
if(argc == 2){
filename = argv[1];
filename.append(".lan");
}
scan(filename);
cout << endl;
cout << "WHAT?!" << endl;
return 0;
}
And here's just the function part of scanner:
void scan(string filename){
char current;
char look;
int currentline = 1;
Type test;
tokenType * testtk;
std::ifstream file(filename.c_str()); // open file
/***scanner creation***/
while (file.good()) // loop while extraction from file file possible
{
int state = 0;
int lookstate = 0;
int type = 0;
int i = 0;
int nextstate;
look = file.peek();
/*** check for comments ***/
if(assignType(look)==5){
current = file.get(); //"current = look" at this point
current = file.get(); //current moves to next character
while(current != 24) {
current = file.get();
}
}
initializeToken(testtk);
while(state >= 0) {
current = file.get(); // get character from file
look = file.peek();
lookstate = state;
if(assignType(current)!=24){ //keeps newlines from printing
testtk->str[i] = current; //put current scanned char into string
}
testtk->tokenId = (tokenIdType) state;
nextstate = table[state][assignType(current)];
state = nextstate;
if(assignType(current)==24) { //keeps track of '\n'
currentline++;
}
if(i<STRSIZ) i++;
}
testtk->line=currentline;
printToken(testtk);
}
file.close();
}
Any help as to what is causing this segmentation fault would be appreciated,
edited
Here is most of my token.h:
typedef enum
{ EOFtk, IDtk, NOTtk, DOTtk, NUMtk, COMMENTtk, ASSIGNtk, EQUALtk, LESSEQtk,
GREATEQtk, PLUStk, MINUStk, MULTtk, DIVtk, MODtk, PARENLtk,
PARENRtk, COMMAtk, CURLYLtk, CURLYRtk, SEMItk, BRACKLtk, BRACKRtk, COLONtk,
LESStk, GREATtk, STARTtk, STOPtk, THENtk, IFtk, IFFtk, WHILEtk, VARtk, INTtk,
FLOATtk, DOtk, READtk, WRITEtk, VOIDtk, RETURNtk, DUMMYtk, PROGRAMtk
} tokenIdType;
typedef enum
{ WS, LETTER, NUMBER, AMBER, NOT, PLUS, MINUS, MULT, DIV,
MOD, EQUAL, LESS, GREATER, UNDERSC, DOT, PARENL,
PARENR, COMMA, CURLYL, CURLYR, SEMI, BRACKL, BRACKR, COLON,
NEWLINE, NONALPHA, EOF
} Type;
typedef struct
{ char str[STRSIZ];
tokenIdType tokenId;
int line;
} tokenType;
and here is some of the other functions in my scanner.cpp:
Type assignType(unsigned char current) {
Type temp;
if((current <= 122 && current >=97) || (current <= 90 && current >=65)){
temp = LETTER;
return temp;
}
if(current <= 57 && current >=48) {
temp = NUMBER;
return temp;
}
if(current == 10) {
temp = NEWLINE;
return temp;
}
switch (current) {
case ' ':
temp = WS;
break;
case '&':
temp = AMBER;
break;
case '!':
temp = NOT;
break;
case '+':
temp = PLUS;
break;
case '-':
temp = MINUS;
break;
case '*':
temp = MULT;
break;
case '/':
temp = DIV;
break;
case '%':
temp = MOD;
break;
case '=':
temp = EQUAL;
break;
case '<':
temp = LESS;
break;
case '>':
temp = GREATER;
break;
case '_':
temp = UNDERSC;
break;
case '.':
temp = DOT;
break;
case '(':
temp = PARENL;
break;
case ')':
temp = PARENR;
break;
case ',':
temp = COMMA;
break;
case '{':
temp = CURLYL;
break;
case '}':
temp = CURLYR;
break;
case ';':
temp = SEMI;
break;
case '[':
temp = BRACKL;
break;
case ']':
temp = BRACKR;
break;
case ':':
temp = COLON;
break;
default :
temp = NONALPHA;
break;
}
return temp;
}
void initializeToken(tokenType *token){
for(int i=0;i<STRSIZ;i++){ //initialize tokenType str
token->str[i]=0;
}
}
void printToken(tokenType *token){
if(token->tokenId != COMMENTtk && token->tokenId != EOFtk) { //ignore comments
cout<<"Line:";
cout.width(3);
cout<<token->line;
cout.flush();
cout<<" TokenID:";
cout.width(3);
cout<<token->tokenId;
cout.flush();
cout<<" String: ";
printf("%s\n", token->str);
}
}
bool isToken(int state, int look){
if(table[state][assignType(look)] >=0)
return false;
else return true;
}
bool compareStr(char x[], char y[]){
int score;
for(int i=0;i<STRSIZ;i++) {
if(x[i] != y[i])
score++;
}
if(score == 0)
return true;
else
return false;
}
I figured out what was causing my Segmentation Fault. I needed to malloc the space for my struct. I added this to my code:
tokenType * testtk = (tokenType *) malloc(sizeof(*testtk));
.
.
.
<existing code>
.
.
.
free(testtk);