title: C++ Out of Memory Exception Test
body: <p>I have written a program for a double-ended queue using an array in C++. One of the requirements of the assignment is to handle exceptions.</p>
  
  <p>One of the exceptions we are supposed to check for is the out of memory exception.</p>
  
  <p>Since it is a dynamically-sized queue I have a method that doubles the size of the array when it gets full. 
  Like this:</p>
  
  <pre><code>try{
      doubleArray();
  } catch (bad_alloc&amp; e) {
      throw bad_alloc();
  }
  </code></pre>
  
  <p>In the main file, this <code>throw</code>s of <code>bad_alloc</code> is caught.</p>
  
  <p>I have no idea how to test if this works or not.
  I'm using a Mac with X-code and also use the Mac terminal to compile and test the files.
  Does anyone know how to test this exception? i.e. set memory limits or something in either of those platforms? Or even if I'm doing it right or not?</p>
  
  <p><strong>EDIT (more info):</strong></p>
  
  <p>This is my implementation of <code>doubleArray</code>:</p>
  
  <pre><code>void Deque&lt;T&gt;:: doubleArray(){
  int newSize = size_of_Deque * 2;
  T *newElement = new T[newSize];
  
  int temp = front;
  int begin = (newSize/4);
  front = begin;
  int end = size_of_Deque + begin - 1;
  
  while (begin&lt;=end) {
      newElement[begin] = element[temp];
      begin++;
      temp = (temp + 1)% size_of_Deque;
  }
  
  element = newElement;
  back = end;
  size_of_Deque = newSize;
  delete [] newElement;
  
  
  }
  </code></pre>
  
  <p>So when the new array (with the double the size of the original array) is allocated that's when there could be the potential for out of memory case. 
  That's why I did:</p>
  
  <pre><code>try{
      doubleArray();
  }catch (bad_alloc&amp; e) {
      throw bad_alloc();
  }
  </code></pre>
  
  <p>Again, my main issue is that I have no idea how to test to see if it works. Is there any way to set memory limits in the terminal or in X-code?</p>