Reputation: 642
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;
}
Upvotes: 0
Views: 153
Reputation: 1847
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.
Upvotes: 3