But if I do it the way I tried before, it won't crash. It's actually an octree and my functions are these: (they are really dirty at the moment, beware )
Code:
class Pointer {
public:
virtual Point3d& split() = 0;
virtual const Point3d& split() const = 0;
virtual Pointer** child() = 0;
virtual const Pointer** child() const = 0;
virtual std::vector<int>& relevantH() = 0;
virtual const std::vector<int>& relevantH() const = 0;
virtual TYPE type() const = 0;
virtual void print() = 0;
virtual void freeTree() = 0;
};
class Node : public Pointer {
// Private members
Point3d splitPoint; // Split point
Pointer* childArray[1<<Dimension]; // Pointers to children
public:
virtual const Point3d& split() const {return splitPoint;}
virtual Point3d& split() {return splitPoint;}
virtual Pointer** child() {return childArray;}
virtual const Pointer** child() const {return (const Pointer**)childArray;}
virtual std::vector<int>& relevantH() {}
virtual const std::vector<int>& relevantH() const {}
virtual TYPE type() const {return TYPE_INNER;}
void print() {
if (child()[0]) child()[0]->print();
std::cout << "Inner\n" << split();
if (child()[1]) child()[1]->print();
if (child()[2]) child()[2]->print();
if (child()[3]) child()[3]->print();
if (child()[4]) child()[4]->print();
if (child()[5]) child()[5]->print();
if (child()[6]) child()[6]->print();
if (child()[7]) child()[7]->print();
}
void freeTree() {
if (child()[0]) child()[0]->freeTree();
if (child()[1]) child()[1]->freeTree();
if (child()[2]) child()[2]->freeTree();
if (child()[3]) child()[3]->freeTree();
if (child()[4]) child()[4]->freeTree();
if (child()[5]) child()[5]->freeTree();
if (child()[6]) child()[6]->freeTree();
if (child()[7]) child()[7]->freeTree();
delete this;
}
};
class Leaf : public Pointer {
// Private members
std::vector<int> relevantHalfspaces; // Relevant halfspaces to leaf
public:
/**
* Constructor of Leaf.
* @param t - size of member vector of leaf
*/
Leaf(const size_t& t, std::vector<int> relH) : relevantHalfspaces(relH) {
// In order to avoid multiple re-allocations
relevantHalfspaces.reserve(t);
// Use only if compiler supports C+11 features. We reserve space,
// we do not create 2*V - 4 objects, but if we do not use all the
// space, it will wait to be allocated, unless we shrink_to_fit.
// relevantHalfspaces.shrink_to_fit(); <-- This is the line of code.
}
virtual const Point3d& split() const {}
virtual Point3d& split() {}
virtual Pointer** child() {return 0;}
virtual const Pointer** child() const {return 0;}
virtual std::vector<int>& relevantH() {return relevantHalfspaces;}
virtual const std::vector<int>& relevantH() const {return relevantHalfspaces;}
virtual TYPE type() const {return TYPE_LEAF;}
void print() {
std::cout<<"Leaf\n";
for( std::vector<int>::const_iterator it =
relevantHalfspaces.begin(); it != relevantHalfspaces.end(); ++it)
std::cout << *it << ' ';
std::cout << std::endl;
}
void freeTree() {
delete this;
}
};
is it safe? Does it delete the tree?
Run it on Linux too and it was ok!
I tried to use valgrind, like this:
linux06:/home/users/std10093/splitReduce>% valgrind --tool=memcheck --leak-check=yes run vertices.txt
%: Too many arguments.
linux06:/home/users/std10093/splitReduce>% valgrind --tool=memcheck --leak-check=yes ./run vertices.txt
%: Too many arguments.
I tried like this too
g++ main.cpp valgrind --leak-check=yes run vertices.txt
g++: error: valgrind: No such file or directory
g++: error: unrecognized option '--leak-check=yes'