Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: None  |  size: 0.76 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to free a structure with arrays of pointers to these structures inside in C  ?
  2. struct Node {
  3.     Node *nodes[MAX]
  4. };
  5.        
  6. Node *full = new Node();
  7. Node *another = new Node();
  8. full->nodes[30] = another;
  9.        
  10. struct Node {
  11.     Node *nodes[MAX];
  12.     Node()
  13.     {
  14.         for (int i = 0; i < MAX; i++)
  15.             nodes[i] = 0;
  16.     }
  17.     ~Node()
  18.     {
  19.         for (int i = 0; i < MAX; i++)
  20.             delete nodes[i];
  21.     }
  22. private:
  23.     // disable copies/assignments
  24.     Node(const Node&);
  25.     Node& operator=(const Node&);
  26. };
  27.        
  28. class Node {
  29. public:
  30.     std::vector<Node*> nodes;
  31.     Node() { nodes.resize(MAX); }
  32.     ~Node() {
  33.        for (std::vector<Node*>::iterator it = nodes.begin(), end = nodes.end(); it != end; ++it) {
  34.            delete *it;
  35.        }
  36.     }
  37. };