Microsoft C ++异常:std :: bad_alloc在内存位置0x002DECB4

时间:2013-12-01 17:03:50

标签: c++

我试图运行这个程序:

Graph::Graph(string file, double th){
    this->_file_name = file;
    _TH = th;
    this->_E_size = 0;
    _V = vector<VertexSet>();
    init();
}

    void Graph::init(){
        ifstream fr(this->_file_name);
        string line;
        getline(fr,line);
        // filling the graph
        while (!line.empty()){
            // implementing tokenizer, witch is : ", "
            // optional: getline(fr,line,",") - as "," a delimiter
            int start = 0, end = 0,current = 0,len = 0;
            double val;
            VertexSet vs = VertexSet();
            char tmp;
            string strVal;
            int size = line.length();
            while (end < size){
                start = current;
                while (current < size && (tmp = line.at(current)) != ',') current ++; // current points now at ','
                if(current == size) break;
                len++;
                end = current;
                current = end + 1;
                strVal = line.substr(start,(end-start));
                val = stod(strVal);
                if (val > _TH){
                    vs.add(len); // adding the current length counter
                    _E_size++;
                }
            }
            getline(fr,line);
            _V.push_back(vs);
        }
    }

问题出现在最后一行“_V.push_back(vs)”中,当程序第一次停止并且消息“std :: bad_alloc at memory location 0x002DECB4”时,我遇到了这个问题。在最后一个对象(712次之后)。 现在这条消息出现在我试图添加到向量的第一个对象上,并显示相同的消息,我不明白。

这是代码的其余部分可能存在问题:

Graph.h:

#include "stdafx.h"

#ifndef GRAPH_H
#define GRAPH_H

#include <sstream>
#include <vector>
#include <fstream>
#include "VertexSet.h"


using namespace std;
class Clique;

class Graph{
private:
    string _file_name;
    vector<VertexSet> _V;
    double _TH;
    int _E_size; //initialize
    bool _mat_flag; //initialize
    void init();
    // clique algorithms def//
    void addbiggerCliQ(vector<VertexSet> &ans, VertexSet curr, VertexSet inter);
    vector<VertexSet> addbiggerCliQ(VertexSet curr, VertexSet inter);
    vector<VertexSet> allEdges();
    void addToSet(vector<VertexSet> ans, vector<Clique> c1);
public:
    Graph(){};// default empty constuctor
    Graph(string file, double th);
    VertexSet Ni(int i);
    void print();
    // clique algorithms def//
    vector<VertexSet> all_cliques(int q_size);
    vector<VertexSet> all_cliques(int min_q_size, int max_q_size);
    vector<VertexSet> all_cliques_of_edge(VertexSet e, int min_q_size, int max_q_size);
    vector<VertexSet> allC(vector<VertexSet> c0);
    VertexSet intersection(VertexSet c);
    vector<VertexSet> all_cliques_DFS(int min_size, int max_size);
    void all_cliques_DFS(string out_file, int min_size, int max_size);
    vector<Clique> allC_seed(Clique edge, int min_size, int max_size);
    void write2file();
};

class Clique {
private:
    VertexSet _clique;
    VertexSet _Ni;
    static Graph _graph;
public:
    Clique(int a, int b);
    Clique(Clique &ot);
    Clique(Clique ot, int vertex);
    int size();
    VertexSet clique();
    void addVertex(int vertex);
    VertexSet commonNi();
    string toFile();
    static void init(Graph &g){_graph=g;}
};

#endif

Clique.cpp:

#include "stdafx.h"
#include "Graph.h"

Graph Clique::_graph;

Clique::Clique(int a, int b){
    _clique = VertexSet();
    _clique.add(a);
    _clique.add(b);
    _Ni = _graph.Ni(a).intersection(_graph.Ni(b));
}

Clique::Clique(Clique &ot){
    _clique =  VertexSet(ot._clique);
    _Ni =  VertexSet(ot._Ni);
}

Clique::Clique(Clique ot, int vertex){
    _clique =  VertexSet(ot._clique);
    _Ni =  VertexSet(ot._Ni);
    this->addVertex(vertex);
}

int Clique::size(){return _clique.size();}

VertexSet Clique::clique(){return this->_clique;}

void Clique::addVertex(int vertex){
    _clique.add(vertex);
    _Ni = _Ni.intersection(_graph.Ni(vertex));
}

VertexSet Clique::commonNi(){return _Ni;}

string Clique::toFile(){
    string s;
    for(int i=0;i<this->_clique.size();i++) {s+=this->_clique.at(i)+",";}
    return s;
}

VertexSet.h:

#include "stdafx.h"

#ifndef VERTEXSET_H
#define VERTEXSET_H


using namespace std;

class VertexSet{
private:
    int *_set;
    int _setSize;
    int _sp;
    void resize();
public:
    const static int INIT_SIZE=20, INC=50;
    VertexSet();
    VertexSet(const VertexSet &ot);
    ~VertexSet();
    void add(int a);
    int size();
    int at(int i);
    string toString();
    string toFile();
    VertexSet intersection(VertexSet ot);
};

#endif

VertexSet.cpp:

#include "stdafx.h"
#include "VertexSet.h"

VertexSet::VertexSet(){
    _set = new int[INIT_SIZE];
    _setSize = INIT_SIZE;
    _sp = 0;
}

VertexSet::VertexSet(const VertexSet &ot){
    _sp = ot._sp;
    _setSize = ot._setSize;
    _set = new int[_setSize];
    for(int i = 0; i < _sp; i++) this->add(ot._set[i]);
}

VertexSet::~VertexSet(){delete []_set;}

void VertexSet::add(int a){
    if(_sp == _setSize) resize();
    _set[_sp] = a;
    _sp++;
}

void VertexSet::resize(){
    int *tmp = new int[_sp+INC];
    _setSize = _sp+INC;
    for(int i=0;i<_sp;i++) tmp[i]=_set[i];
    this->_set=tmp;
}

int VertexSet::size(){return _sp;}

int VertexSet::at(int i){return _set[i];}

string VertexSet::toString(){
    string ans = "Set: |"+this->size()+'| ';
        for(int i=0;i<_sp;i++) ans+=this->at(i)+", ";
    return ans;
}

string VertexSet::toFile(){
    string ans;
        for(int i=0;i<_sp;i++) ans+=this->at(i)+", ";
    return ans;
}

VertexSet VertexSet::intersection(VertexSet ot){
    VertexSet ans;
    int i=0,j=0;
    while(i<_sp && j < ot.size()) {
        int a1=this->at(i), a2 = ot.at(j);
        if(a1==a2) {
            ans.add(a1); i++; j++;}
        else if(a1<a2) i++;
        else j++;
    }
    return ans;
}

0 个答案:

没有答案