我正在开发一个目标是创建自动机的项目。
自动机由结构定义:
typedef struct {
int num_states;
int initial_state;
State * states;
} Automaton;
State是另一个定义状态之间弧的结构:
typedef struct {
int num_arcs;
bool is_final;
Arc * arcs;
} State;
typedef struct {
int symbol;
int destination;
} Arc;
我用malloc创建了一个自动机:
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;
}
那么我想使用这些函数创建状态和弧来获取2个自动机:
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++;
}
我想将这两个自动机合二为一,所以我写了这个函数:
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;
}
然而,我可以创建自动机,向它添加状态和弧没有任何问题,当我尝试将它们与append_automaton合并在一起时我在add_state()中得到分段错误我重新分配状态以适应新的状态自动机。
所以我的问题如下:为什么在这个函数(append_automaton)中realloc给我一个分段错误,虽然它在它之外完美地工作?
PS:copy_Automaton()确实会覆盖create_Automaton(),所以我删除了append_automaton()中的行Automaton * a = create_automaton()
这是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;
}
答案 0 :(得分:3)
我看到的问题是你在for循环后正在更新a->num_states
。但是a->num_states
它在函数add_state(a);
的循环内使用
你需要把(a-&gt; num_states)++放在循环中。