title: c++ solution fails on leetcode with misaligned address error but runs in visual studio
body: <p>i'm new to C++ so i'm trying some leetcode problems. i'm currently attempting the min stack problem. i think i've solved it correctly, except i get the following runtime error from leetcode:</p>
  
  <pre><code>Line 24: Char 37: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment (solution.cpp)
  </code></pre>
  
  <p>here is my visual studio code, with tests - the error originates from the following line in push():</p>
  
  <pre><code>                topStackNode-&gt;stackPrev = newNode;
  </code></pre>
  
  <p>anyone know what would cause this to happen? i read a bit and some people with similar errors say its because ListNode is not initialized to NULL, but i clearly initialize all ListNodes to NULL in my struct. thanks!</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  using namespace std;
  // design a stack that supports push, pop, top, and retrieving the minimum element in constant time
  
  
  class MinStack {
  
  struct ListNode {
      ListNode* stackNext;
      ListNode* stackPrev;
      ListNode* minNext;
      ListNode* minPrev; 
      int val;
      ListNode(int v) : val(v), stackNext(NULL), stackPrev(NULL), minNext(NULL), minPrev(NULL) 
      {
      }
  };
  
  public:
      /** initialize your data structure here. */
      MinStack() {}
  
      void push(int x) {
  
          ListNode* newNode = new ListNode(x); 
  
          if (topStackNode == NULL)
              topStackNode = newNode;
          else {
              topStackNode-&gt;stackPrev = newNode;
              newNode-&gt;stackNext = topStackNode;
              topStackNode = newNode; 
          }
  
          insertNodeMinStack(newNode); 
      }
  
      void pop() {
          removeFromMinStack(topStackNode);
          popFromStack();
      }
  
      void popFromStack() {
          topStackNode = topStackNode-&gt;stackNext; 
      }
  
      void removeFromMinStack(ListNode* node) { // delete the currently popped node from the min stack
          if (node != NULL) {
              if (node == topMinNode) {
                  topMinNode = node-&gt;minNext;
                  min = topMinNode-&gt;val; 
                  return; 
              }
              if (node-&gt;minPrev != NULL)
                  node-&gt;minPrev-&gt;minNext = node-&gt;minNext; 
              if (node-&gt;minNext != NULL)
                  node-&gt;minNext-&gt;minPrev = node-&gt;minPrev;  
          }
  
      }
  
      int top() {
          if (topStackNode != NULL)
              return topStackNode-&gt;val;
          else 
              return NULL; 
      }
  
      int getMin() {
          return min; 
      }
  
      void insertNodeMinStack(ListNode* node) {
          if (topMinNode == NULL) { // inserting node on an empty list
              topMinNode = node; 
          }
          else if (node-&gt;val &lt; topMinNode-&gt;val) { // node has smallest value
              topMinNode-&gt;minPrev = node;
              node-&gt;minNext = topMinNode;
              topMinNode = node; // set new top min node and update min
              min = node-&gt;val; 
          }
          else {
              ListNode* currentNode = topMinNode;
              while (currentNode-&gt;minNext != NULL &amp;&amp; currentNode-&gt;val &lt; node-&gt;val) {
                  currentNode = currentNode-&gt;minNext;
              }
              if (currentNode-&gt;minNext == NULL) { // reached end of list, 'node' has largest value in list
                  currentNode-&gt;minNext = node;
                  node-&gt;minPrev = currentNode; 
                  //min remains unchanged
              }
              else { // we're somewhere in the middle of the list, ie there are nodes surronding 'node'
                  node-&gt;minNext = currentNode-&gt;minNext; 
                  node-&gt;minPrev = currentNode; 
                  currentNode-&gt;minNext = node;
                  node-&gt;minNext-&gt;minPrev = node; 
              }
          }
      }
  
  private: 
      int min;
      ListNode* topStackNode;
      ListNode* topMinNode; 
  };
  
  int main() {//1,2,6,3,4,5,6
      MinStack* ms = new MinStack(); 
  
      ms-&gt;push(5); 
      ms-&gt;push(3);
      ms-&gt;pop(); 
      ms-&gt;push(10);
      ms-&gt;push(-3);
      ms-&gt;pop();
      ms-&gt;pop();
      ms-&gt;push(-11);
  
      cout &lt;&lt; "minstack min = " &lt;&lt; ms-&gt;getMin() &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>EDIT - new solution below using shared pointers:</p>
  
  <pre><code>class MinStack {
      struct ListNode {
          shared_ptr&lt;ListNode&gt; prev;
          shared_ptr&lt;ListNode&gt; next;
          shared_ptr&lt;ListNode&gt; minNext;
          shared_ptr&lt;ListNode&gt; minPrev; 
          int value;
          ListNode(int val) : prev(nullptr), next(nullptr), minNext(nullptr), minPrev(nullptr), value(val) {}
      };
  
  public:
  
      shared_ptr&lt;ListNode&gt; topn;
      shared_ptr&lt;ListNode&gt; minTop; 
      shared_ptr&lt;ListNode&gt; node;
  
      MinStack() : topn(nullptr), minTop(nullptr), node(nullptr){}
  
       void push(int value) {
  
          // cout &lt;&lt; "pushing value " &lt;&lt; value &lt;&lt; endl; 
  
          if (topn == nullptr) {
              topn = make_shared&lt;ListNode&gt;(value);
              insertToMinList();
          }
          else {
              node.reset(); 
              node = make_shared&lt;ListNode&gt;(value);
              node-&gt;next = topn;
              topn = node;
              insertToMinList(); 
          }
      }
  
       void removeFromMinList() { 
       //removing the node topn from min list
           if (topn-&gt;minNext != nullptr &amp;&amp; topn-&gt;minPrev != nullptr) {
              // cout &lt;&lt; "removing, neither null, " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
               topn-&gt;minNext-&gt;minPrev = topn-&gt;minPrev;
               topn-&gt;minPrev-&gt;minNext = topn-&gt;minNext; 
           }
           else if (topn-&gt;minNext != nullptr) {
              // cout &lt;&lt; "removing, next not null, " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
               topn-&gt;minNext-&gt;minPrev = topn-&gt;minPrev;
           }
           else if (topn-&gt;minPrev != nullptr) {
              // cout &lt;&lt; " removing, prev not null, " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
               topn-&gt;minPrev-&gt;minNext = topn-&gt;minNext;
           }
           else {
              // cout &lt;&lt; "no condition met in removign " &lt;&lt; endl; 
           }
           if (topn == minTop) {
               minTop = topn-&gt;minNext;
           }
  
       }
  
       void insertToMinList() { 
           if (minTop == nullptr) {
               minTop = topn;
               //cout &lt;&lt; "min list null, initializing with " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
           }
           else if (topn-&gt;value &lt;= minTop-&gt;value) {
               //cout &lt;&lt; "new value is smallest " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
               minTop-&gt;minPrev = topn; 
               topn-&gt;minNext = minTop; 
               minTop = topn; 
           }
           else {
               //cout &lt;&lt; "searching list to place value " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
               shared_ptr&lt;ListNode&gt; temp = make_shared&lt;ListNode&gt;(minTop-&gt;value); 
               temp-&gt;minNext = minTop-&gt;minNext; 
  
               while (temp != nullptr &amp;&amp; temp-&gt;value &lt; topn-&gt;value) { //go down the list
                   topn-&gt;minNext = temp-&gt;minNext; 
                   topn-&gt;minPrev = temp;
                   temp = temp-&gt;minNext; 
               }
               //while loop completes. now, temp is either nullptr or between two nodes
               if (temp == nullptr) {// new value is largest
                   //cout &lt;&lt; "reached end of list, assigning value " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
                   topn-&gt;minPrev-&gt;minNext = topn; 
                   topn-&gt;minNext = nullptr; 
               }
               else { // between two nodes, reassign prev and next
                   //cout &lt;&lt; "in middle of list, assigning vale " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
                   topn-&gt;minNext-&gt;minPrev = topn;
                   topn-&gt;minPrev-&gt;minNext = topn; 
               }
           }
       }
  
      void pop() {
  
          //cout &lt;&lt; "popping value " &lt;&lt; topn-&gt;value &lt;&lt; endl; 
          removeFromMinList(); 
          topn = topn-&gt;next;
  
  
      }
  
      int top(){
          return topn-&gt;value;
      }
  
      int getMin() {
          return minTop-&gt;value; 
      }
  };
  </code></pre>