2D矢量分割故障:push_back

时间:2017-04-28 23:18:53

标签: c++ vector segmentation-fault

我正在尝试解决处理 2d向量数组的问题.FunClass获取两个参数,集合数和每组行数。 问题是 update_mytable函数 em> stackoverflow 我读到,在大多数情况下,seg-fault的问题是因为我们试图访问一个不存在的vector.But我不认为我在这里有同样的问题。

using std::vector;
typedef vector<mytable_entry>::iterator mytable_iter;

struct mytable_entry
{   
    int x_entry;
    int y_entry;
};

mytable_entry new_mytable_entry(int new_ip, int new_target) {
    mytable_entry new_entry;
    new_entry.ip_entry = new_ip;
    new_entry.target_entry = new_target;

    return new_entry;
}

class FunClass : public BaseClass
{
public:
    FunClass(unsigned mytable_sets, unsigned mytable_lines_per_set)
         : table_sets(mytable_sets), table_assoc(mytable_lines_per_set)
    {
        mytable_table = vector< vector<mytable_entry> > (table_sets,vector< mytable_entry> (table_assoc));

    }

    ~FunClass() {
        /* Vectors will be decontructed automatially out of scope */
    }

    virtual bool predict(int x, int y) {
        /* If mytable_entry exist return true, else false */
        unsigned mytable_set_index = x % table_sets;

        if (find_mytable_entry(mytable_table[mytable_set_index])) {
            return true;
        } else {
            return false;
        }
    }

    virtual void update(bool val1, bool val2, int x, int y) {
        unsigned mytable_set_index = x % table_sets;

        if (val1 == val2){
            update_mytable_table(mytable_table[mytable_set_index],ip,target_entry,table_assoc);
        }

    void update_mytable(vector<mytable_entry> & mytable_set, int x, int y,unsigned table_assoc){
        mytable_entry new_entry = new_mytable_entry(ip,target);

        mytable_set.push_back(new_entry);
        if (mytable_set.size() > table_assoc){
            mytable_set.erase(mytable_set.begin());
        }
    }

    bool find_mytable_entry(vector<mytable_entry> & mytable_set, int x) {
        mytable_iter iter;
        for (iter = mytable_set.begin(); iter != mytable_set.end(); iter++){
            if ((*iter).ip_entry == ip) {
                mytable_entry tmp_mytable_entry = (*iter);

                mytable_set.erase(iter);
                mytable_set.push_back(tmp_mytable_entry);
                return true;
            }
        }
        return false;
    }

private:
    vector< vector<mytable_entry> > mytable_table;

    unsigned table_sets, table_assoc;
};

如果我从update_mytable中删除 mytable_set.push_back(new_entry); ,那么一切顺利。

0 个答案:

没有答案