
Untitled
By: a guest on
May 25th, 2012 | syntax:
None | size: 0.76 KB | hits: 15 | expires: Never
How to free a structure with arrays of pointers to these structures inside in C ?
struct Node {
Node *nodes[MAX]
};
Node *full = new Node();
Node *another = new Node();
full->nodes[30] = another;
struct Node {
Node *nodes[MAX];
Node()
{
for (int i = 0; i < MAX; i++)
nodes[i] = 0;
}
~Node()
{
for (int i = 0; i < MAX; i++)
delete nodes[i];
}
private:
// disable copies/assignments
Node(const Node&);
Node& operator=(const Node&);
};
class Node {
public:
std::vector<Node*> nodes;
Node() { nodes.resize(MAX); }
~Node() {
for (std::vector<Node*>::iterator it = nodes.begin(), end = nodes.end(); it != end; ++it) {
delete *it;
}
}
};