1

我有以下代码:

class Translator
{
typedef property<symbol_t,string,property <message_t,string> > edge_properties;
typedef property<vertex_name_t,string, property<vertex_index_t,int,property<vertex_index1_t,bool> > > vertex_properties;
typedef adjacency_list<listS,listS,directedS,vertex_properties,edge_properties> Graph;
typedef property_map<Graph,vertex_name_t> :: type name_id ;
typedef graph_traits<Graph>::vertex_descriptor vert_descript;
vector<vert_descript> Process_initial_state;
vector<Graph> Processes;
vector<name_id> Process_state_name;

Translator(const char *path)
{
    generate_automata(path);
    print_things();
}

void print_things()
{
    vert_descript vert;
    for(int i=0;i<Process_initial_state.size();i++)
    cout<<endl<<Process_state_name[i][Process_initial_state[i]];
}
    void generate_automata(const char *path)
{

    xml_document xml;
    xml_parse_result xml_result = xml.load_file(path);
    xml_node temp = xml.first_child();
    graph_traits<Graph>::vertex_descriptor vertex;
    graph_traits<Graph>::vertex_descriptor from_vertex;
    graph_traits<Graph>::vertex_descriptor to_vertex;
for(temp = temp.child("role");temp;temp = temp.next_sibling("role"))
    {
        //Adding states to the Graph
        string role = temp.attribute("name").value();
        Graph process;
        name_id state_name = get(vertex_name,process);
        vert_descript istate;
        vector<vert_descript > fstates;
        for(xml_node temp1 = temp.child("states").child("state");temp1;temp1 = temp1.next_sibling())
        {
            string state = temp1.child_value();
            state = role + "_" + state;
            vertex = add_vertex(process);
            state_name[vertex] = state;
            if(temp1.attribute("type") &&  (!strcmp(temp1.attribute("type").value(),"initial")))
            {
            istate = vertex;
            }
            if(temp1.attribute("type") && (!strcmp(temp1.attribute("type").value(),"final")))
            fstates.push_back(vertex);
        }
    }
Process_initial_state.push_back(istate);
Process_state_name.push_back(state_name);
}
}
};

现在,我在 print_things 中遇到了分段错误。如果我只是打印最后一个初始状态的 state_name ......那么它工作正常......但如果我尝试从第一个初始状态打印,那么我会遇到分段错误。为什么会这样。。

4

1 回答 1

1

我认为该变量state_name是外for循环的本地变量。退出循环后,变量被销毁,并且您将对已释放内存的引用放入向量中。如果我正确阅读了您的代码,您应该state_name在退出循环之前推入您的向量。

于 2013-06-18T13:59:35.377 回答