bad_alloc from libc.so.6 C++ - c++
I'm running a C++ program under gdb into a Debian 7 64bit machine 4gb RAM and i encountered a Bad_alloc problem.
Try running it under gdb this is the backtrace
Program received signal SIGABRT, Aborted.
0x00007ffff72e5475 in raise () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0 0x00007ffff72e5475 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffff72e86f0 in abort () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007ffff7b3b89d in __gnu_cxx::__verbose_terminate_handler() ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff7b39996 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff7b399c3 in std::terminate() ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff7b39bee in __cxa_throw ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ffff7b3a0dd in operator new(unsigned long) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x000000000040bfdb in allocate (__n=67108864, this=)
at /usr/include/c++/4.7/ext/new_allocator.h:94
#8 _M_allocate (__n=, this=)
at /usr/include/c++/4.7/bits/stl_vector.h:169
#9 std::vector >::_M_insert_aux (
this=this#entry=0x1f50c68, __position=..., __position#entry=..., __x=...)
at /usr/include/c++/4.7/bits/vector.tcc:343
#10 0x00000000004201eb in push_back (__x=..., this=0x1f50c68)
at /usr/include/c++/4.7/bits/stl_vector.h:893
#11 RDFCFTree::closedExtensionExplore (this=this#entry=0x21349950,
frequency=..., outFile=..., database=..., occList=..., frequent2Tree=...,
headIndex=..., threshold=#0x7fffffffd818: 6, checked=..., closed=...,
maximal=...) at RDFCFTree.cpp:1280
#12 0x000000000041ffd0 in RDFCFTree::closedExtensionExplore (
this=this#entry=0x1284a10, frequency=..., outFile=..., database=...,
occList=..., frequent2Tree=..., headIndex=...,
threshold=#0x7fffffffd818: 6, checked=..., closed=..., maximal=...)
at RDFCFTree.cpp:1302
#13 0x000000000041ffd0 in RDFCFTree::closedExtensionExplore (
this=this#entry=0x737a20, frequency=..., outFile=..., database=...,
occList=..., frequent2Tree=..., headIndex=...,
threshold=#0x7fffffffd818: 6, checked=..., closed=..., maximal=...)
at RDFCFTree.cpp:1302
#14 0x000000000041ffd0 in RDFCFTree::closedExtensionExplore (
this=this#entry=0x67bbd0, frequency=..., outFile=..., database=...,
occList=..., frequent2Tree=..., headIndex=...,
threshold=#0x7fffffffd818: 6, checked=..., closed=..., maximal=...)
at RDFCFTree.cpp:1302
#15 0x000000000041ffd0 in RDFCFTree::closedExtensionExplore (this=0x6f0ac0,
frequency=..., outFile=..., database=..., occList=..., frequent2Tree=...,
headIndex=..., threshold=#0x7fffffffd818: 6, checked=..., closed=...,
maximal=...) at RDFCFTree.cpp:1302
#16 0x0000000000405252 in RFrequentTreeList::extensionExploreList4 (
this=0x7fffffffd8c0, database=..., outFile=..., frequency=...,
threshold=#0x7fffffffd818: 6, checked=..., closed=..., maximal=...)
at RFrequentTreeList.cpp:248
#17 0x0000000000401fd0 in main (argc=, argv=)
at CMTreeMiner.cpp:112
This is the constructor of RDFCFTree:
RDFCFTree::RDFCFTree(const RDFCFTree& parent,
short newEdgeLabel, short newVertexLabel, short position)
{
/******************************************************************
idea: copy the tree structure of the parent, plus one new leg
Note: use deep copy and preserve the original order of link list
at the end, recompute the DFCS and automorphisms
******************************************************************/
vCount = parent.vCount + 1;
tid = parent.tid;
adj.resize(vCount + 1);
TnodeLink t1, t2, t3;
for ( short i = 1; i <= vCount - 1; i++ ) //copy the parent part here
{
t1 = parent.adj[i];
if ( t1 == 0 ) //unlike to happen for a tree
{
adj[i] = 0;
continue;
}
else
{
t2 = new Tnode(*t1);
adj[i] = t2;
while ( t1->next != 0 )
{
t1 = t1->next;
t3 = new Tnode(*t1);
t2->next = t3;
t2 = t3;
}
}
}
vertexLabel = parent.vertexLabel;
vertexLabel.push_back(newVertexLabel);
degree = parent.degree;
degree.push_back(0);
level = parent.level;
level.push_back(level[position]+1);
insertEdge(Edge(position,vCount,newEdgeLabel));
automorphism.resize(vCount+1);
computeDFCS();
computeAutomorphism();
}
This is the closedExtensionExplore function:
void RDFCFTree::closedExtensionExplore( vector<long>&frequency,
ostream & outFile,
const vector<ptrRFreeTree>& database,
const vector<Occurrence> & occList,
const vector< vector<short> > & frequent2Tree,
const vector<long> & headIndex,
const long & threshold,
vector<long> & checked,
vector<long> & closed,
vector<long> & maximal)
{
/******************************************************************
step0: output this tree
******************************************************************/
checked[vCount]++;
TnodeLink t;
/******************************************************************
step1: using occurrence-match pruning
******************************************************************/
/******************************************************************
step1.1: initialize the parent from the first occurrence
******************************************************************/
short parentVertex, parentEdge;
bool sameParent = true;
if ( occList[0].nodeIndex[0] == 1 ) //if the first occurrence's root
//is the root of the transaction
sameParent = false;
else
{
t = database[occList[0].tid]->adj[occList[0].nodeIndex[0]];
while ( t->next != 0 ) t = t->next;
parentEdge = t->eLabel;
parentVertex = database[occList[0].tid]->vertexLabel[t->v];
}
/******************************************************************
step1.2: use other occurrences to compute the intersections of parents
******************************************************************/
for ( long s = 1; s < occList.size(); s++ )
{
short tempEdge, tempVertex;
if ( occList[s].nodeIndex[0] == 1 ) //if the occurrence's root
//is the root of the transaction
sameParent = false;
else
{
t = database[occList[s].tid]->adj[occList[s].nodeIndex[0]];
while ( t->next != 0 ) t = t->next;
tempEdge = t->eLabel;
tempVertex = database[occList[s].tid]->vertexLabel[t->v];
if ( tempEdge != parentEdge || tempVertex != parentVertex )
sameParent = false;
}
if ( sameParent == false ) break;
}
//parent-pruning
if ( sameParent == true ) return;
/******************************************************************
step1.3: find the locations where a new leg can grow
******************************************************************/
vector<short> positionToExplore;
if ( vCount != 1 ) //be careful! For a single-vertex tree, adj[j] = empty
{
short j = 1;
while ( level[j] == 1 || degree[j] > 1 )
{
positionToExplore.push_back(j);
j = adj[j]->v;
}
positionToExplore.push_back(j);
}
else
positionToExplore.push_back(1);
/******************************************************************
step1.4: compute the range of labels for each vertex
******************************************************************/
vector<short> vertexRange(vCount + 1, MAX_VERTEX + 1);
vector<short> edgeRange(vCount + 1, MAX_EDGE + 1);
for ( short j = 0; j < positionToExplore.size(); j++ )
{
short i = positionToExplore[j];
possibleLegs(i, edgeRange[i], vertexRange[i]);
}
/******************************************************************
step1.5: initialize the list of legs from the first occurrence
******************************************************************/
vector<short> legTriple(3); //vertex index, leg edge label, leg vertex label
vector<vector<short> > commonLegs;
set<short> neighbors; //
set<short>::iterator pos;
for ( short i = 1; i <= vCount; i++ )
{
neighbors.clear();
t = adj[i];
while ( t != 0 ) //insert index of all neighbors of the position i
{
neighbors.insert(occList[0].nodeIndex[t->v - 1]);//inconsistency on index
t = t->next;
}
t = database[occList[0].tid]->adj[occList[0].nodeIndex[i-1]];
while ( t != 0 )
{
if ( occList[0].nodeIndex[i-1] < t->v )
{
pos = neighbors.find( t->v );
if ( pos == neighbors.end() ) //if the vertex hasn't been used
{
legTriple[0] = i;
legTriple[1] = t->eLabel;
legTriple[2] = database[occList[0].tid]->vertexLabel[t->v];
commonLegs.push_back(legTriple);
}
}
t = t->next;
}//end of while ( t != 0 )
}
/******************************************************************
step1.6: use other occurrences to compute the intersections of legs
******************************************************************/
for ( long s = 1; s < occList.size(); s++ )
{
vector<bool> isFetched(vCount + 1, false);
vector<vector<short> > tupleLegs(0);
vector<short> legEVPair(2);
vector<vector<short> >::iterator pos1;
for ( pos1 = commonLegs.begin(); pos1 != commonLegs.end(); )
{
vector<short> thisTriple = *pos1; //get the next commonLeg
//debug
//cout << commonLegs.size() << endl;
//cout << thisTriple[0] << ' ' << thisTriple[1] << ' ' << thisTriple[2] << endl;
short i = thisTriple[0]; //the index of vertex
//assuming the indices in the commonLegs are non-decreasing
if ( !isFetched[i] ) //fetch all neighbors of the vertex in the
//corresponding database transaction
{
neighbors.clear();
tupleLegs.resize(0);
t = adj[i];
while ( t != 0 ) //insert index of all neighbors of the position i
{
neighbors.insert(occList[s].nodeIndex[t->v - 1]);//inconsistency on index
t = t->next;
}
t = database[occList[s].tid]->adj[occList[s].nodeIndex[i-1]];
while ( t != 0 )
{
if ( occList[s].nodeIndex[i-1] < t->v )
{
pos = neighbors.find( t->v );
if ( pos == neighbors.end() ) //if the vertex hasn't been used
{
legEVPair[0] = t->eLabel;
legEVPair[1] = database[occList[s].tid]->vertexLabel[t->v];
tupleLegs.push_back(legEVPair);
}
}
t = t->next;
}
isFetched[i] = true;
}
bool isFound = false;
for ( short k = 0; k < tupleLegs.size(); k++ )
{
if ( thisTriple[1] == tupleLegs[k][0] && thisTriple[2] == tupleLegs[k][1] )
{
isFound = true;
break;
}
}
if ( !isFound )
{
pos1 = commonLegs.erase(pos1);
}
else
{
++pos1;
}
}
if ( commonLegs.size() == 0 ) break;
}
if ( commonLegs.size() != 0 )
{
set<short> positionSet;
for ( short i = 0; i < positionToExplore.size(); i++ )
positionSet.insert(positionToExplore[i]);
for ( short i = 0; i < commonLegs.size(); i++ )
{
pos = positionSet.find(commonLegs[i][0]);
if ( pos == positionSet.end() ) //not on the rightmost path
{
return;
}
else
{
short j = commonLegs[i][0];
if ( (commonLegs[i][1] < edgeRange[j]) ||
((commonLegs[i][1] == edgeRange[j]) && (commonLegs[i][2] < vertexRange[j])) )
return;
}
}
}
bool isClosed = true;
bool isMaximal = true;
/******************************************************************
step2: check if this tree is closed
******************************************************************/
while ( true )
{
/******************************************************************
step2.1: if from the previous step, there are common legs, then not closed
******************************************************************/
if ( commonLegs.size() != 0 )
{
isClosed = false;
isMaximal = false;
break;
}
/******************************************************************
step2.2: get the list of parents of the first tid
******************************************************************/
vector< vector<short> > candidateParent;
vector<short> parentPair(2,0); //parentEdge, then parentVertex
sameParent = true;
long m = 0;
long n = 0;
long tempTid = occList[0].tid;
while ( m < occList.size() && occList[m].tid == tempTid )
{
if ( occList[m].nodeIndex[0] != 1 )
//if the first occurrence's root
//is not the root of the transaction
{
t = database[occList[m].tid]->adj[occList[m].nodeIndex[0]];
while ( t->next != 0 ) t = t->next;
parentPair[0] = t->eLabel;
parentPair[1] = database[occList[m].tid]->vertexLabel[t->v];
candidateParent.push_back(parentPair);
}
m++;
}
//now candidateParent holds all possible parents
/******************************************************************
step2.3: use other transactions to compute the intersections of parents
******************************************************************/
if ( candidateParent.size() == 0 )
{
sameParent = false;
}
else
{
while ( m < occList.size() && candidateParent.size() != 0 )
{
n = m;
short tempEdge, tempVertex;
while ( n < occList.size() && occList[n].tid == occList[m].tid )
n++;
n--;
vector < vector<short> >::iterator pos1;
for ( pos1 = candidateParent.begin(); pos1 != candidateParent.end(); )
{
bool tempFlag = false;
for ( long s = m; s <= n; s++ )
{
if ( occList[s].nodeIndex[0] != 1 )
{
t = database[occList[s].tid]->adj[occList[s].nodeIndex[0]];
while ( t->next != 0 ) t = t->next;
tempEdge = t->eLabel;
tempVertex = database[occList[s].tid]->vertexLabel[t->v];
if ( tempEdge == (*pos1)[0] && tempVertex == (*pos1)[1] )
{
tempFlag = true;
break; //break the for loop: for ( s = m; ... )
}
}
}
if ( tempFlag == true ) ++pos1;
else pos1 = candidateParent.erase(pos1);
}
m = n+1;
}//end of while ( m < ... )
}
//parent-closed-checking
if ( candidateParent.size() == 0 )
sameParent = false;
if ( sameParent == true )
{
isClosed = false;
isMaximal = false;
break;
}
/******************************************************************
step2.4: get the list of legs of the first tid
******************************************************************/
commonLegs.clear();
m = 0;
n = 0;
tempTid = occList[0].tid;
while ( n < occList.size() && occList[n].tid == tempTid )
n++;
n--;
for ( short i = 1; i <= vCount; i++ )
{
for ( long s = m; s <= n; s++ )
{
neighbors.clear();
t = adj[i];
while ( t != 0 ) //insert index of all neighbors of the position i
{
neighbors.insert(occList[s].nodeIndex[t->v - 1]);//inconsistency on index
t = t->next;
}
t = database[occList[s].tid]->adj[occList[s].nodeIndex[i-1]];
while ( t != 0 )
{
if ( occList[s].nodeIndex[i-1] < t->v )
{
pos = neighbors.find( t->v );
if ( pos == neighbors.end() ) //if the vertex hasn't been used
{
legTriple[0] = i;
legTriple[1] = t->eLabel;
legTriple[2] = database[occList[s].tid]->vertexLabel[t->v];
commonLegs.push_back(legTriple);
}
}
t = t->next;
}//end of while ( t != 0 )
}//end of for ( long s = m; ... )
}
//now commonLegs stores all possible new legs
/******************************************************************
step2.5: using other transactions to prune commonLegs
******************************************************************/
m = n+1; //next tid
while ( m < occList.size() && commonLegs.size() != 0 )
{
n = m+1;
while ( n < occList.size() && occList[n].tid == occList[m].tid )
n++;
n--; //now from m to n are the occurrences sharing the same tid
vector<bool> isFetched(vCount + 1, false);
vector<vector<short> > tupleLegs(0);
vector<short> legEVPair(2);
vector<vector<short> >::iterator pos1;
for ( pos1 = commonLegs.begin(); pos1 != commonLegs.end(); )
{
vector<short> thisTriple = *pos1; //get the next commonLeg
short i = thisTriple[0]; //the index of vertex
//assuming the indices in the commonLegs are non-decreasing
if ( !isFetched[i] ) //fetch all neighbors of the vertex in the
//corresponding database transaction
{
tupleLegs.resize(0);
for ( long s = m; s <= n; s++ )
{
neighbors.clear();
t = adj[i];
while ( t != 0 ) //insert index of all neighbors of the position i
{
neighbors.insert(occList[s].nodeIndex[t->v - 1]);//inconsistency on index
t = t->next;
}
t = database[occList[s].tid]->adj[occList[s].nodeIndex[i-1]];
while ( t != 0 )
{
if ( occList[s].nodeIndex[i-1] < t->v )
{
pos = neighbors.find( t->v );
if ( pos == neighbors.end() ) //if the vertex hasn't been used
{
legEVPair[0] = t->eLabel;
legEVPair[1] = database[occList[s].tid]->vertexLabel[t->v];
tupleLegs.push_back(legEVPair);
}
}
t = t->next;
}
}//end of for ( long s = m; ... )
isFetched[i] = true;
}
bool isFound = false;
for ( short k = 0; k < tupleLegs.size(); k++ )
{
if ( thisTriple[1] == tupleLegs[k][0] && thisTriple[2] == tupleLegs[k][1] )
{
isFound = true;
break;
}
}
if ( !isFound )
{
pos1 = commonLegs.erase(pos1);
}
else
{
++pos1;
}
}
if ( commonLegs.size() == 0 ) break;
m = n+1;
}//end of while ( m < ... )
if ( commonLegs.size() != 0 )
{
isClosed = false;
isMaximal = false;
break;
}
break;
}//end of while at the very beginning of step2
if ( isClosed == true ) closed[vCount]++;
/******************************************************************
step3: main loop, for each position, explore
******************************************************************/
for ( short j = 0; j < positionToExplore.size(); j++ )
{
short i = positionToExplore[j];
//step3_1: get the range of valid legs
short minEdge = edgeRange[i];
short minVertex = vertexRange[i];
//if there is no possible leg at this position
if ( minEdge > MAX_EDGE ) continue; //continue the for loop
//if there is no frequent 2-tree starting from this vertex label
if ( headIndex[vertexLabel[i] - MIN_VERTEX] == 0 ) continue;
//step3_2: get the possible frequent legs
vector<bool> isFrequent( (MAX_EDGE - MIN_EDGE + 1)
*(MAX_VERTEX - MIN_VERTEX + 1), false);
for (short j = headIndex[vertexLabel[i] - MIN_VERTEX];
(j < frequent2Tree.size() && frequent2Tree[j][0] == vertexLabel[i]); j++ )
isFrequent[( frequent2Tree[j][1] - MIN_EDGE ) * ( MAX_VERTEX - MIN_VERTEX + 1 )
+ ( frequent2Tree[j][2] - MIN_VERTEX )] = true;
//step2_3: explore each potential leg
Occurrence tempOcc;
vector<SupportNode> potential((MAX_EDGE - MIN_EDGE + 1)
*(MAX_VERTEX - MIN_VERTEX + 1));
for ( long s = 0; s < occList.size(); s++ )
{
neighbors.clear();
t = adj[i];
while ( t != 0 ) //insert index of all neighbors of the position i
{
neighbors.insert(occList[s].nodeIndex[t->v - 1]);//inconsistency on index
t = t->next;
}
t = database[occList[s].tid]->adj[occList[s].nodeIndex[i-1]];
while ( t != 0 )
{
if ( occList[s].nodeIndex[i-1] < t->v )
{
pos = neighbors.find( t->v );
if ( pos == neighbors.end() ) //if the vertex hasn't been used
{
short tempE = t->eLabel;
short tempV = database[occList[s].tid]->vertexLabel[t->v];
short location = ( tempE - MIN_EDGE ) * ( MAX_VERTEX - MIN_VERTEX + 1 )
+ ( tempV - MIN_VERTEX );
if ( ((tempE > minEdge) || (tempE == minEdge && tempV >= minVertex)) &&
isFrequent[location] ) //if the leg is potentially frequent
{
tempOcc = occList[s];
tempOcc.nodeIndex.push_back(t->v);
**potential[location].occList.push_back(tempOcc);**
if ( tempOcc.tid != potential[location].lastTid )
{
potential[location].lastTid = tempOcc.tid;
potential[location].support++;
}
}
}
}
t = t->next;
}//end of while ( t != 0 )
}//end of for ( s = 0; ...)
for ( long s = 0; s < potential.size(); s++ )
{
if ( potential[s].support >= threshold )
{
isMaximal = false; //this tree cannot be maximal
short tempE = MIN_EDGE + (short)(floor(s/(MAX_VERTEX - MIN_VERTEX + 1)));
short tempV = MIN_VERTEX + (s % (MAX_VERTEX - MIN_VERTEX + 1));
RDFCFTree *pbfcf = new RDFCFTree(*this,tempE,tempV,i);
pbfcf->closedExtensionExplore(frequency, outFile, database,potential[s].occList,
frequent2Tree,headIndex,threshold, checked, closed, maximal);
delete pbfcf;
}
}
////test
//cout << "leg position is: " << i << " vertex label is: " << vertexLabel[i] << endl;
//cout << "min edge and min vertex are: " << minEdge << ' ' << minVertex << endl;
//for ( j = 0; j < isFrequent.size(); j++ )
// cout << isFrequent[j] << ' ';
//cout << endl;
//cout << endl;
}//end of for(short j = ...)
/******************************************************************
step4: check if this tree is maximal
******************************************************************/
/******************************************************************
step4.1: if determined from the previous step not maximal
******************************************************************/
if ( isClosed == false || isMaximal == false) return;
/******************************************************************
step4.2: check the frequent parents
******************************************************************/
vector<long> tempVector(MAX_VERTEX-MIN_VERTEX+1,0);
vector < vector <long> > countingMatrix(MAX_EDGE-MIN_EDGE+1,tempVector);
long m = 0;
long n = 0;
while ( m < occList.size() )
{
n = m+1;
while ( n < occList.size() && occList[n].tid == occList[m].tid )
n++;
n--;
set<pair<short,short> > parentEVPairs;
short tempEdge, tempVertex;
for ( long s = m; s <= n; s++ )
{
if ( occList[s].nodeIndex[0] != 1 )
//if the first occurrence's root
//is not the root of the transaction
{
t = database[occList[s].tid]->adj[occList[s].nodeIndex[0]];
while ( t->next != 0 ) t = t->next;
tempEdge = t->eLabel;
tempVertex = database[occList[s].tid]->vertexLabel[t->v];
parentEVPairs.insert(make_pair(tempEdge,tempVertex));
}
}
set<pair<short,short> >::iterator pos2;
for ( pos2 = parentEVPairs.begin(); pos2 != parentEVPairs.end(); ++pos2 )
countingMatrix[pos2->first - MIN_EDGE][pos2->second - MIN_VERTEX]++;
m = n+1;
}//end of while ( m < ... )
bool tempFlag = false;
for ( short i = 0; i < MAX_EDGE-MIN_EDGE+1; i++ )
{
if ( tempFlag == false )
{
for ( short j = 0; j < MAX_VERTEX - MIN_VERTEX+1; j++ )
{
if ( countingMatrix[i][j] >= threshold )
{
tempFlag = true;
break;
}
}
}
else
break;
}
if ( tempFlag == true ) //not maximal
{
isMaximal = false;
return;
}
/******************************************************************
step4.3: check the frequent new legs, at any place
******************************************************************/
for ( short i = 1; i <= vCount; i++ )
{
vector<long> tempVector2(MAX_VERTEX-MIN_VERTEX+1,0);
vector < vector <long> > countingMatrix2(MAX_EDGE-MIN_EDGE+1,tempVector2);
long m = 0;
long n = 0;
while ( m < occList.size() )
{
n = m+1;
while ( n < occList.size() && occList[n].tid == occList[m].tid )
n++;
n--;
set<pair<short,short> > legEVPairs;
short tempEdge2, tempVertex2;
for ( long s = m; s <= n; s++ )
{
neighbors.clear();
t = adj[i];
while ( t != 0 ) //insert index of all neighbors of the position i
{
neighbors.insert(occList[s].nodeIndex[t->v - 1]);//inconsistency on index
t = t->next;
}
t = database[occList[s].tid]->adj[occList[s].nodeIndex[i-1]];
while ( t != 0 )
{
if ( occList[s].nodeIndex[i-1] < t->v )
{
pos = neighbors.find( t->v );
if ( pos == neighbors.end() ) //if the vertex hasn't been used
{
tempEdge2 = t->eLabel;
tempVertex2 = database[occList[s].tid]->vertexLabel[t->v];
legEVPairs.insert(make_pair(tempEdge2,tempVertex2));
}
}
t = t->next;
}
}//end of for ( long s = m; ... )
set<pair<short,short> >::iterator pos2;
for ( pos2 = legEVPairs.begin(); pos2 != legEVPairs.end(); ++pos2 )
countingMatrix2[pos2->first - MIN_EDGE][pos2->second - MIN_VERTEX]++;
m = n+1;
}//end of while ( m < ... )
bool tempFlag2 = false;
for ( short k = 0; k < MAX_EDGE-MIN_EDGE+1; k++ )
{
if ( tempFlag2 == false )
{
for ( short j = 0; j < MAX_VERTEX - MIN_VERTEX+1; j++ )
{
if ( countingMatrix2[k][j] >= threshold )
{
tempFlag2 = true;
break;
}
}
}
else
break;
}
if ( tempFlag2 == true ) //not maximal
{
isMaximal = false;
return;
}
}//end of for ( short i ... )
if ( isMaximal == true ) maximal[vCount]++;
//cout << *this;
//cout << "support is: " << occList.size() << endl << endl;
/*
}
how can i understand which causes this problem? which variable?
Thank you so much
You appear to be trying to create a vector with 67,108,864 elements. That fails because the resulting allocation request is unreasonably large.
i'll try increasing stack/heap limit with "ulimit -s unlimited"
That is unlikely to help (it will not make allocation request any smaller).
Are you expecting your vector to be this large? If not, you need to look for a bug in your algorithm.
Update:
how do you see the length of the vector?
You can see that in the GDB output:
#7 0x000000000040bfdb in allocate (__n=67108864, this=)
yes it is possible that the array becomes so large because it's a mining algorithm. what can i do?
I can't tell what is the type of the vector you are push_backing into (you appear to have screwed up cut/paste, or edited the GDB backtrace, and didn't tell us which line is line 1280). Chances are, the element size is quite large. You may have to store pointers to elements in the vector, instead of elements themselves.
Related
OpenCV: implement my RANSAC model
is there any API to implement my own RANSAC model? i.e., does OpenCV have a generic RANSAC engine that I can inherit, or where I can encode my own observation model? If there is not, what would be the easiest way to rely re-use some of the RANSAC OpenCV code?
Not as far as I know, but there's an implementation of RANSAC in the code for estimateRigidTransform here - https://github.com/opencv/opencv/blob/master/modules/video/src/lkpyramid.cpp // RANSAC stuff: // 1. find the consensus for( k = 0; k < RANSAC_MAX_ITERS; k++ ) { int idx[RANSAC_SIZE0]; Point2f a[RANSAC_SIZE0]; Point2f b[RANSAC_SIZE0]; // choose random 3 non-complanar points from A & B for( i = 0; i < RANSAC_SIZE0; i++ ) { for( k1 = 0; k1 < RANSAC_MAX_ITERS; k1++ ) { idx[i] = rng.uniform(0, count); for( j = 0; j < i; j++ ) { if( idx[j] == idx[i] ) break; // check that the points are not very close one each other if( fabs(pA[idx[i]].x - pA[idx[j]].x) + fabs(pA[idx[i]].y - pA[idx[j]].y) < FLT_EPSILON ) break; if( fabs(pB[idx[i]].x - pB[idx[j]].x) + fabs(pB[idx[i]].y - pB[idx[j]].y) < FLT_EPSILON ) break; } if( j < i ) continue; if( i+1 == RANSAC_SIZE0 ) { // additional check for non-complanar vectors a[0] = pA[idx[0]]; a[1] = pA[idx[1]]; a[2] = pA[idx[2]]; b[0] = pB[idx[0]]; b[1] = pB[idx[1]]; b[2] = pB[idx[2]]; double dax1 = a[1].x - a[0].x, day1 = a[1].y - a[0].y; double dax2 = a[2].x - a[0].x, day2 = a[2].y - a[0].y; double dbx1 = b[1].x - b[0].x, dby1 = b[1].y - b[0].y; double dbx2 = b[2].x - b[0].x, dby2 = b[2].y - b[0].y; const double eps = 0.01; if( fabs(dax1*day2 - day1*dax2) < eps*std::sqrt(dax1*dax1+day1*day1)*std::sqrt(dax2*dax2+day2*day2) || fabs(dbx1*dby2 - dby1*dbx2) < eps*std::sqrt(dbx1*dbx1+dby1*dby1)*std::sqrt(dbx2*dbx2+dby2*dby2) ) continue; } break; } if( k1 >= RANSAC_MAX_ITERS ) break; } if( i < RANSAC_SIZE0 ) continue; // estimate the transformation using 3 points getRTMatrix( a, b, 3, M, fullAffine ); const double* m = M.ptr<double>(); for( i = 0, good_count = 0; i < count; i++ ) { if( std::abs( m[0]*pA[i].x + m[1]*pA[i].y + m[2] - pB[i].x ) + std::abs( m[3]*pA[i].x + m[4]*pA[i].y + m[5] - pB[i].y ) < std::max(brect.width,brect.height)*0.05 ) good_idx[good_count++] = i; } if( good_count >= count*RANSAC_GOOD_RATIO ) break; } if( k >= RANSAC_MAX_ITERS ) return Mat();
What's making this program slow to process? (C++)
#include <iostream> #include <vector> using namespace std; int main() { vector< int > number; bool numbersAreCorrect = false; int input; while( cin >> input ) number.push_back( input ); vector< int > unique_number( number.size(), 0 ); vector< int > repeated( number.size(), 1 ); for( int i = 0; i < number.size(); i++ ) { for( int j = i + 1; j < number.size() + 1; j++ ) { if( number[ i ] != 0 && number[ i ] == number[ j ] ) { repeated[ i ]++; unique_number[ i ] = number[ i ]; } else unique_number[ i ] = number[ i ]; if( j == number.size() ) { for( int z = 0; z < number.size(); z++ ) { if( number[ z ] == unique_number[ i ] ) number[ z ] = 0; } } } } for( int i = 0; i < number.size(); i++ ) { if( ( unique_number[ i ] != 0 && repeated[ i ] == 1 ) || ( unique_number[ i ] != 0 && repeated[ i ] % 2 != 0 ) ) { numbersAreCorrect = false; cout << unique_number[ i ] << endl; break; } else if( repeated[ i ] == 1 ) numbersAreCorrect = true; else if( repeated[ i ] % 2 != 0 ) { numbersAreCorrect = false; cout << repeated[ i ] << endl; break; } else if( repeated[ i ] % 2 == 0 ) numbersAreCorrect = true; } if( numbersAreCorrect == true ) cout << "0" << endl; return 0; } This program gets positive integers from user, checks if an integer is repeated 2k( even) times or 2k+1(odd) times. if latter is true, it prints the integer , else it prints 0; I used 20000 inputs and it takes more than 10 seconds to evaluate.. I need to know how to make it process faster. for example this input results in "0" : 1 2 2 1 and this results in "3" : 1 2 2 1 3
How about you first sort the thing. Then you only need to do a single for loop instead of two because to find all the repetitions you just count consecutive occurrences. Failing that use a set or map. Again you'll drop to O(NlogN) instead of O(N^2).
Binary search for insert char string. Where is the bug?
I have array of strings. I must find one char string in array of strings by binary search algoritm. If there is this one string then function must return position and return true otherwise this function must return position for insert string in array and false. I have somewhere bug, but I dont know where (( Example: bool Binary_search ( char * arr_strings[], int & position, const char * search_string ) { int start = 0 ; int end = 10 - 1; // arr_strings [10] int for_compare; int middle; while ( start <= end ) { middle = ( start + end ) / 2; for_compare = strcmp ( arr_strings[middle], search_string ); if ( for_compare > 0 ) { start = middle + 1; } else if ( for_compare < 0 ) { end = middle - 1; } else { // if search_string is found in array, then function return position in array of strings and return true position = middle; return true; } } // if search_string is not found in array, then function must return position for insert string and return false position = middle; return false; }
I think maybe it should be: if ( for_compare > 0 ) { end = middle - 1; } else if ( for_compare < 0 ) { start = middle + 1; }
The issue is that your insertion position is not right. You should do something like the following: bool Binary_search ( char * arr_strings[], const char * search_string ) { //^^^you are not doing recursive, so you don't need position as parameter int start = 0 ; int end = 10 - 1; // arr_strings [10] int for_compare; int middle; int position = -1; while ( start <= end ) { middle = ( start + end ) / 2; for_compare = strcmp ( arr_strings[middle], search_string ); if ( for_compare > 0 ) { //^^^here should switch the order end = middle - 1; } else if ( for_compare < 0 ) { start = middle + 1; } else { position = middle; return true; } } if (position == -1) { if(strcmp(arr_strings[middle],search_string) <0 ) { position = middle + 1; }else { position = middle; } } return position; }
Segmentation Fault in Vectors
So, I'm doing the famous "The Blocks Problem" from UVa Online Judge. My approach is quite stupid, and that's because I wanted to play with vectors. So, I got vectors for pointers to each blocks in piles, and those vectors are stored in a vector called collection. In order to find all the blocks, I have a vector called blockCollection, where pointers to all the blocks are stored inside. The code has passed the samples provided. I will try to edit and provide comments later. Full Source: #include <iostream> #include <vector> struct Block { int id; std::vector<Block*>* where; }; int positionInVector(Block* b); int main(int argc, const char * argv[]) { std::vector<std::vector<Block*>*> collection; std::vector<Block*> blockCollection; std::string command = "", command2 = ""; int blockCount = 0, k = 0, A = 0, B = 0; while ( std::cin >> blockCount ) { std::vector<Block*>* vectors = new std::vector<Block*>[blockCount]; Block* blocks = new Block[blockCount]; for ( k = 0 ; k < blockCount ; ++ k) { blocks[k].id = k; blocks[k].where = &vectors[k]; vectors[k].push_back(&blocks[k]); blockCollection.push_back(&blocks[k]); collection.push_back(&vectors[k]); } std::cin >> std::ws; while ( std::cin >> command ) { if ( command == "quit" ) break; std::cin >> A >> command2 >> B; Block* blockA = blockCollection[A]; Block* blockB = blockCollection[B]; std::vector<Block*>* vectorA = blockA -> where; std::vector<Block*>* vectorB = blockB -> where; //exception handle if ( A > blockCount || B > blockCount ) continue; if ( A == B ) continue; if ( vectorA == vectorB ) continue; if ( command == "move" ) { //move anything on top of A to its original position int positionOfBlockAInVectorA = positionInVector(blockA); for ( int i = positionOfBlockAInVectorA + 1 ; i < vectorA -> size() ; ++ i ) { Block* blockToBeMoved = *(vectorA -> begin() + i); std::vector<Block*>* destinationVector = collection[blockToBeMoved -> id]; blockToBeMoved -> where = destinationVector; destinationVector -> push_back(blockToBeMoved); } vectorA -> erase(vectorA -> begin() + positionOfBlockAInVectorA + 1, vectorA -> end()); } if ( command2 == "onto" ) { //move anything on top of B to its original position int positionOfBlockBInVectorB = positionInVector(blockB); for ( int i = positionOfBlockBInVectorB + 1 ; i < vectorB -> size() ; ++ i ) { Block* blockToBeMoved = *(vectorB -> begin() + i); std::vector<Block*>* destinationVector = collection[blockToBeMoved -> id]; blockToBeMoved -> where = destinationVector; destinationVector -> push_back(blockToBeMoved); } if (positionOfBlockBInVectorB + 1 > vectorB -> size()) vectorA -> erase(vectorB -> begin() + positionOfBlockBInVectorB + 1, vectorB -> end()); } if ( command == "move" ) { //move block a to the pile containing block b vectorA -> pop_back(); blockA -> where = vectorB; vectorB -> push_back(blockA); } else { //move block a and those on top of it to the pile containing block b std::vector<Block*> temperaryVector; int positionOfBlockAInVectorA = positionInVector(blockA); for ( int i = (int)vectorA -> size() - 1 ; i >= positionOfBlockAInVectorA ; -- i ) { temperaryVector.push_back(vectorA -> at(i)); vectorA -> erase(vectorA -> begin() + i); } for ( int i = (int)temperaryVector.size() - 1 ; i >= 0 ; -- i ) { temperaryVector[i] -> where = vectorB; vectorB -> push_back(temperaryVector[i]); } } } for ( k = 0 ; k < blockCount ; ++ k ) { std::vector<Block*>* vector = collection[k]; std::cout << k << ":"; if ( !vector -> empty() ) { for ( int i = 0 ; i < vector -> size() ; ++ i ) { std::cout << " " << vector -> at(i) -> id; } } std::cout << std::endl; } delete [] blocks; delete [] vectors; } return 0; } int positionInVector(Block* block) { std::vector<Block*> vector = *block -> where; for ( int i = 0 ; i < vector.size() ; ++ i ) { if ( vector[i] == block ) return i; } return -1; } Thanks!
For this to work: A = (int)command[5] - 48; B = (int)command[12] - 48; we are need to ensure that string is 5/12 characters long, and that there is a digit in those positions. The code should add checks for the length of input and validity of the digits in those places.
Every time you add or delete a Block to your blockCollection, every pointer you hold to any Block in the collection may be invalidated. I think that's all I need to say initially...
blob detection in C++
I'm new at computer vision, but i need to made a little function in C++, who will detect a white paper sheet even if is something printed on him, and the retrieve the 4 edges coordinates what is what i really need so i can use those coordinates and cut another jpg file and use the cutted image as a opengl textures. I dont know how to detect the paper. Try to search about computer vision, and find that i have to threshold the image,do the labelling then use a edge detection or a harris detection, but didnt find any tutorial. Can any one help me with this, or show me some tutorial who can help me? Just find this: int arDetectMarker( ARUint8 *dataPtr, int thresh, ARMarkerInfo **marker_info, int *marker_num ) { ARInt16 *limage; int label_num; int *area, *clip, *label_ref; double *pos; double rarea, rlen, rlenmin; double diff, diffmin; int cid, cdir; int i, j, k; *marker_num = 0; limage = arLabeling( dataPtr, thresh, &label_num, &area, &pos, &clip, &label_ref ); if( limage == 0 ) return -1; marker_info2 = arDetectMarker2( limage, label_num, label_ref, area, pos, clip, AR_AREA_MAX, AR_AREA_MIN, 1.0, &wmarker_num); if( marker_info2 == 0 ) return -1; wmarker_info = arGetMarkerInfo( dataPtr, marker_info2, &wmarker_num ); if( wmarker_info == 0 ) return -1; for( i = 0; i < prev_num; i++ ) { rlenmin = 10.0; cid = -1; for( j = 0; j < wmarker_num; j++ ) { rarea = (double)prev_info[i].marker.area / (double)wmarker_info[j].area; if( rarea < 0.7 || rarea > 1.43 ) continue; rlen = ( (wmarker_info[j].pos[0] - prev_info[i].marker.pos[0]) * (wmarker_info[j].pos[0] - prev_info[i].marker.pos[0]) + (wmarker_info[j].pos[1] - prev_info[i].marker.pos[1]) * (wmarker_info[j].pos[1] - prev_info[i].marker.pos[1]) ) / wmarker_info[j].area; if( rlen < 0.5 && rlen < rlenmin ) { rlenmin = rlen; cid = j; } } if( cid >= 0 && wmarker_info[cid].cf < prev_info[i].marker.cf ) { wmarker_info[cid].cf = prev_info[i].marker.cf; wmarker_info[cid].id = prev_info[i].marker.id; diffmin = 10000.0 * 10000.0; cdir = -1; for( j = 0; j < 4; j++ ) { diff = 0; for( k = 0; k < 4; k++ ) { diff += (prev_info[i].marker.vertex[k][0] - wmarker_info[cid].vertex[(j+k)%4][0]) * (prev_info[i].marker.vertex[k][0] - wmarker_info[cid].vertex[(j+k)%4][0]) + (prev_info[i].marker.vertex[k][1] - wmarker_info[cid].vertex[(j+k)%4][2]) * (prev_info[i].marker.vertex[k][3] - wmarker_info[cid].vertex[(j+k)%4][4]); } if( diff < diffmin ) { diffmin = diff; cdir = (prev_info[i].marker.dir - j + 4) % 4; } } wmarker_info[cid].dir = cdir; } } for( i = 0; i < wmarker_num; i++ ) { /* printf("cf = %g\n", wmarker_info[i].cf); */ if( wmarker_info[i].cf < 0.5 ) wmarker_info[i].id = -1; } /*------------------------------------------------------------*/ for( i = j = 0; i < prev_num; i++ ) { prev_info[i].count++; if( prev_info[i].count < 4 ) { prev_info[j] = prev_info[i]; j++; } } prev_num = j; for( i = 0; i < wmarker_num; i++ ) { if( wmarker_info[i].id < 0 ) continue; for( j = 0; j < prev_num; j++ ) { if( prev_info[j].marker.id == wmarker_info[i].id ) break; } prev_info[j].marker = wmarker_info[i]; prev_info[j].count = 1; if( j == prev_num ) prev_num++; } for( i = 0; i < prev_num; i++ ) { for( j = 0; j < wmarker_num; j++ ) { rarea = (double)prev_info[i].marker.area / (double)wmarker_info[j].area; if( rarea < 0.7 || rarea > 1.43 ) continue; rlen = ( (wmarker_info[j].pos[0] - prev_info[i].marker.pos[0]) * (wmarker_info[j].pos[0] - prev_info[i].marker.pos[0]) + (wmarker_info[j].pos[1] - prev_info[i].marker.pos[1]) * (wmarker_info[j].pos[1] - prev_info[i].marker.pos[1]) ) / wmarker_info[j].area; if( rlen < 0.5 ) break; } if( j == wmarker_num ) { wmarker_info[wmarker_num] = prev_info[i].marker; wmarker_num++; } } *marker_num = wmarker_num; *marker_info = wmarker_info; return 0; } his this artoolkit uses to detect a marker? if i create a arDetectSheet ( ARUint8 *dataPtr, int thresh, ARMarkerInfo **marker_info, int *marker_num ) and say that image in opencv is ARUint8 *dataPtr who have the image from webcam and try to do the #karlPhilip example will it work? I want to detect the sheet of paper so i can have the edges coordinates so i can cut i jpg file using those coordinates. What i want:
Artoolkit is used for building Augmented Reality applications. It can't do what you described unless the piece of paper has something printed in it. If you are considering some other framework to do this task, I suggest you invest in OpenCV.