tags:

views:

147

answers:

1

I'm working on a simple lex program for class, and in it I'm creating a very rudimentary symbol table, just an array of strings with a linear scan for search. I've declared it as:

char* identifiers[100];

And I'm using it like so:

found = false;
for (i = 0; i < seen_identifiers; i++) {
    if (!strcmp(identifiers[i], yytext)) {
        printf("Identifier \"%s\" already in symbol table", yytext);
        found = true;
        break;
    }
}
if (!found) {
    printf("identifier: %s\n", yytext);
    seen_identifiers++;
    identifiers[seen_identifiers] = yytext;
}

However I consistently get a segfault at the strcmp call. I'm sure I've screwed up something super simple.

+4  A: 
seen_identifiers++;
identifiers[seen_identifiers] = yytext;

If seen_identifiers starts at 0, you never assign to identifiers[0] and so the strcmp will fault.

Steve Gilham
... I feel like an idiot, this is what I get for writing in high level languages all day.
Alex Gaynor
Nice observation. I was thinking of null termination.
erelender