2

I am writing a compiler project. I created an AST tree, where every node is defined by structure:

typedef struct node {
   char type[10];
   char *string;
   int number_of_children;

   struct node *children[];
} node;

I wanted to create an array of symbols of undefined length. I created structure, to store name of the symbol and it's address in the memory:

typedef struct symbol {
   char *name;
   int address;
} symbol;

and then I created array of undefined length:

symbol *symbols_table[];

I created function to add new symbol:

void add_symbol(node *p) {
    symbols_table[number_of_symbols] = malloc(sizeof(symbol));
    symbols_table[number_of_symbols]->name = malloc(sizeof(p->string));

    strcpy(symbols_table[number_of_symbols]->name, p->string);
    symbols_table[number_of_symbols]->address = memory_pointer;

    memory_pointer++;
    number_of_symbols++;
}

where p is node from the AST tree and memory_pointer is needed to give address in the memory to each symbol.

And here is the problem. When I want to add only 2 symbols to the symbols_table everything is working fine. But when I want to add 3 and more it gives segmentation fault. Do you have any ideas, why is it happening?

3
  • 5
    What do you think the declaration symbol *symbols_table[] creates? If you think it creates an array that can hold any number of elements you might choose to store in it, you are sadly mistaken. Commented Jan 3, 2016 at 9:38
  • I thought as you said. So this must be it. I have to change this array. Thank you very much :) Commented Jan 3, 2016 at 9:40
  • This definition symbol *symbols_table[];, if done globally, should make the compiler issue a warning, if given local to a function it should not even compile. Take warnings serious. Commented Jan 3, 2016 at 13:02

1 Answer 1

2

The problem, as I see here is with

 symbols_table[number_of_symbols]->name = malloc(sizeof(p->string));

in your code, string is a pointer. So, sizeof(p->string)) will not give you the size of the allocated memory, it will give you the size of the pointer itself. At later point, when you do strcpy() it overruns the allocated memory an creates undefined behavior.

What you want instead is

 symbols_table[number_of_symbols]->name = malloc(strlen(p->string) + 1);

to get the proper memory allocation.

On a differnt approach, you can also have a look at strdup() to avoid the whole malloc() + strcpy() thing.

Sign up to request clarification or add additional context in comments.

3 Comments

+1, although strdup is not standard C, so it depends on OP's platform whether they can use it or not.
@szczurcio Right. That is why I've put that as a note, rather. :)
Thank you for quick answer. I tried both solutions, but it didn't work :/ I don't think, that the problem is in the other part of program, but I may be wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.