0

I'm working on a project where the goal is to create automatons.

Automatons are defined by a struct :

typedef struct {
    int num_states;
    int initial_state;
    State * states;
} Automaton;

And State is another struct defining arcs between states:

typedef struct {
    int num_arcs;
    bool is_final;
    Arc * arcs;
} State;

typedef struct {
    int symbol;
    int destination;
} Arc;

I create an automaton with malloc as such :

Automaton* create_automaton(void) {
    Automaton * a = (Automaton *)malloc(sizeof(Automaton));
    assert(a != NULL);
    a->initial_state = UNDEFINED;
    a->num_states = 0;
    a->states = NULL;
    return a;
}

So then I want to take 2 Automatons with states and arcs created with these functions :

int add_state(Automaton* a) {
    State* state = (State *)realloc(a->states, (a->num_states + 1) * sizeof(State));
    if(state == NULL)
        exit(EXIT_FAILURE);
    a->states = state;
    a->states[a->num_states].num_arcs = 0;
    a->states[a->num_states].is_final = FALSE;
    a->states[a->num_states].arcs = NULL;

    return a->num_states++;
}

void add_arc(Automaton* a, int from, int to, int symbol) {
    if(from >= a->num_states || to >= a->num_states)
        exit(EXIT_FAILURE);
    Arc * arc = (Arc *)realloc(a->states[from].arcs, (a->states[from].num_arcs + 1) * sizeof(Arc));
    if(arc == NULL)
        exit(EXIT_FAILURE);
    a->states[from].arcs = arc;
    a->states[from].arcs[a->states[from].num_arcs].destination = to;
    a->states[from].arcs[a->states[from].num_arcs].symbol = symbol;
    a->states[from].num_arcs++;
}

I want to combine these 2 Automatons in one so I wrote this function:

Automaton* append_automaton(Automaton * a1, Automaton * a2)
{
    Automaton * a = copy_automaton(a1);
    int i = 0;
    for(i = 0; i < a2->num_states; i++)
    {
        add_state(a);
        a->states[a1->num_states + i] = a2->states[i];
        for(j = 0;j<a->states->num_arcs;j++)
        {
            a->states[i].arcs[j].destination =+ a2->num_states;
        }
    }
    a->initial_state = a1->initial_state;

    return a;
}

However I can create the Automaton, add states and arcs to it without any problem, when i try to merge them together with append_automaton I get a segmentation fault when in add_state() I realloc State to fit one more state in the new automaton.

So my question is the following : Why is realloc giving me a segmentation fault when in this function (append_automaton) althought it works perfectly outside of it?

PS: copy_Automaton() does indeed overwrite the create_Automaton() so I removed the line: Automaton * a = create_automaton() in append_automaton() And here is copy_automaton():

Automaton* copy_automaton(Automaton* a) {
    int i = 0;
    Automaton * cp_a = malloc(sizeof(Automaton));
    cp_a->states = malloc(sizeof(a->states));
    for(i = 0; i < a->num_states; i++)
    {
        cp_a->states[i].arcs = malloc(sizeof(a->states[i].arcs));
        cp_a->states[i] = a->states[i];
    }
    cp_a->num_states = a->num_states;
    cp_a->initial_state = a->num_states;
    //memcpy(a, cp_a, sizeof(Automaton));
    return cp_a;
}
4
  • Can you provide a minimal example non working input that segfault? Also you said add_arc() segfault, but that function is never called. Commented Oct 10, 2015 at 13:24
  • sorry i meant add_state() segfault not add_arc() Commented Oct 10, 2015 at 13:59
  • In append_automaton() the assignment a = copy_automaton(a1); wipes out the previous value of a = create_automaton(); (the initialiser) Commented Oct 10, 2015 at 14:06
  • Yes indeed I updated my post to fix this but it doesn't really matter since copy_automaton() just overwrites the one created with create_automaton() Commented Oct 10, 2015 at 15:07

1 Answer 1

3

The problem I see is your are updating a->num_states after the for cycle. However a->num_states it is used inside the cycle in the function add_state(a); You need to put (a->num_states)++ inside the loop.

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

1 Comment

Yes thank you I didn't see that mistake but I still get seg fault with the realloc when I add a state. The problem is if I update a->num_states in the for loop num_states increments by 2 because add_state also increment num_state. So I just removed num_state incrementation in append_automaton.

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.