title: Why does ofstream mess up my pointers?
body: <pre><code>void Heap::printHeap(string file) {
    string heapString = "graph {\n";
    for (int i = 1; i &lt; heapSize; i++) {
    Node parentNode = heap[i];
    heapString += parentNode.nodeToDotFormat() + "\n";
  
    int leftChildIndex = 2 * i;
    int rightChildIndex = 2 * i + 1;
    if (leftChildIndex &lt; heapSize) {
      heapString += parentNode.nodeToDotFormat();
      heapString += "--";
  
      Node leftChild = heap[leftChildIndex];
      heapString += leftChild.nodeToDotFormat();
      heapString += "\n";
  
      if (rightChildIndex &lt; heapSize) {
        heapString += parentNode.nodeToDotFormat();
        heapString += "--";
  
        Node rightChild = heap[rightChildIndex];
        heapString += rightChild.nodeToDotFormat();
        heapString += "\n";
      }
    }
    }
    heapString += "}";
    ofstream out(file);
    out &lt;&lt; heapString;
    out.close();
  }
  </code></pre>
  
  <p>I'm trying to print my heap. And the heaps consist of struct of Nodes with pointers. For some reason though when the <code>ofstream</code> gets executed down below the code it messes up the pointers of my heaps. What could be causing this? And how would I fix it.</p>
  
  <p>This is what the points look like before <code>ofstream</code> line is executed:</p>
  
  <p><a href="https://i.stack.imgur.com/HvlhG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HvlhG.png" alt="enter image description here"></a></p>
  
  <p>This is what it looks like after <code>ofstream</code> gets executed:
  <a href="https://i.stack.imgur.com/pWi5P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pWi5P.png" alt="enter image description here"></a></p>
  
  <p>As you can see it changed the left child letter and weight, and the right child letter weight. Why do you think ofstream causes this behavior?</p>
  
  <p>Here is the Node struct:</p>
  
  <pre><code>struct Node {
  char letter;
  int weight;
  string nodeInString() {
    string nodeString = "{'";
    char copyletter = letter;
    nodeString.append(&amp;copyletter);
    nodeString += "', ";
    nodeString += to_string(weight);
    nodeString += "}";
    return nodeString;
  };
  string nodeToDotFormat() {
    string dotString = "";
    char copyletter = letter;
    dotString.append(&amp;copyletter);
    dotString += "_";
    dotString += to_string(weight);
    return dotString;
  }
  
  Node *leftChild;
  Node *rightChild;
  };
  </code></pre>