0

im trying to run this program:

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);
        }
    }

The problem is in the last line "_V.push_back(vs)", i had two problems with this line the first time the program stops with the message "std::bad_alloc at memory location 0x002DECB4." at the last object (after 712 times). Now this message appears at the first object that i'm trying to add to the vector and display the same message and i don't understand way.

This is the rest of the code maybe the problem is there:

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;
}
  • This probably isn't the problem, but names that begin with an underscore followed by a capital letter (`_V`, `_E_size`, `_TH`) are reserved to the implementation. Don't use them. – Pete Becker Dec 01 '13 at 17:05
  • 1
    Well it's a lot of code to understand, but taking a guess I would say that you need to define an assignment operator for `VertexSet`. When you push back a `VertexSet` the assignment operator may be called by `vector` (if it has to reallocate). You don't have a valid assignment operator so your program crashes. You might want to look at the rule of three, http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – john Dec 01 '13 at 18:07
  • Narrow it down please. Show us your testcase. You can't debug this monstrosity without a headache. – Lightness Races in Orbit Dec 01 '13 at 21:28

0 Answers0