title: Exception thrown at 0x7A12FF80 (ucrtbased.dll) in Project 3.exe: 0xC0000005: Access violation reading location 0x00000000
body: <p>I have attempted to run this code. Prior to running this, no warnings or errors exist but once it is executed I have an exception thrown and it stops the program from compiling. Here is my code and the error is in the subject line. The CDA file is being used as header file to create a Circular Dynamic Array that is going to be manipulated in Heaps.cpp. The heaps.cpp is to create a binary heap that is to be used in to create Binomial heaps, but that code has not been developed yet. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#include &lt;iostream&gt;
  using namespace std;
  
  template &lt;class T&gt;
  class CDA
  {
  private:
   int rear;
   int size;
   int capacity;
   T* circArray;
   int front;
   bool ordered;
   T placeHolder;
  
  public:
   CDA();
   CDA(int s);
   ~CDA();
   int Front();
   T Data(int n);
   T&amp; operator[](int i);
   void AddEnd(T v);
   void AddFront(T v);
   void DelEnd();
   void DelFront();
   int Length();
   int Capacity();
   int Clear();
   bool Ordered();
   int SetOrdered();
   int S01(int n);
   T Select(int k);
   void InsertionSort();
   void QuickSort();
   void QuickSort1(int low, int high);
   void CountingSort(int m);
   int Search(T e);
   void reSize();
   void Shrink();
   int BinarySearch(int left, int right, T e);
   int QSortPartition(int low, int high);
   void Swap(int* x, int* y);
   int QSelPartition(int front, int rear);
   T QuickSelect(int front, int rear, int k);
   CDA&lt;T&gt;&amp; operator=(const CDA&amp; a);
   CDA(const CDA&amp; old);
   int Median(int low, int high);
  };
  
  template &lt;class T&gt;
  CDA&lt;T&gt;::CDA()
  {
   capacity = 1;
   circArray = new T[capacity];
   size = 0;
   rear = size - 1;
   front = -1;
   ordered = false;
   placeHolder = 0;
  }
  
  template &lt;class T&gt;
  CDA&lt;T&gt;::CDA(int s)
  {
   size = s;
   capacity = s;
   circArray = new T[capacity];
   front = 0;
   rear = size - 1;
   ordered = false;
  }
  
  template &lt;class T&gt;
  CDA&lt;T&gt;::CDA(const CDA&amp; a)
  {
   size = a.size;
   capacity = a.capacity;
   circArray = new T[a.capacity];
   front = a.front;
   rear = a.size - 1;
   ordered = a.ordered;
   for (int i = a.front; i &lt; a.front + (a.size); i++)
   {
    circArray[i % capacity] = a.circArray[i % capacity];
   }
  }
  
  template &lt;class T&gt;
  CDA&lt;T&gt;::~CDA()
  {
   delete[]circArray;
  }
  
  
  template &lt;class T&gt;
  T&amp; CDA&lt;T&gt;::operator[](int i)
  {
   if (i &gt; capacity)
   {
    cout &lt;&lt; "Array index is out of bounds; exiting." &lt;&lt; endl;
    placeHolder = i;
    cout &lt;&lt; endl;
  
    return placeHolder;
    exit(0);
   }
   else
   {
    return circArray[(front + i) % capacity];
   }
  }
  
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::AddEnd(T v)
  {
   size++;
   if (front == -1)
   {
    circArray[0] = v;
    front++;
    rear++;
    return;
   }
  
   if (size &gt; capacity)
   {
    reSize();
   }
  
   else if (front == -1)
   {
    front = 0;
    rear = size - 1;
   }
  
   else
   {
    rear = (rear + 1) % capacity;
   }
   circArray[rear] = v;
  
  }
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::AddFront(T v)
  {
   size++;
   if (size &gt; capacity)
   {
    reSize();
   }
  
   if (front == -1) //means the array is empty
   {
    front = 0;
    rear = capacity % size;
   }
  
   else if (front == 0) //means something is in spot 0
   {
    front = capacity - 1; //puts front at the end and places the input there
   }
  
   else //go until it is back at zero
   {
    front--;
   }
   circArray[front] = v;
  
  }
  
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::DelEnd()
  {
   size--;
   if (size &lt;= capacity / 4)
   {
    Shrink();
   }
  
   else if (rear == front)
   {
    front = -1;
    rear = -1;
   }
  
   else
   {
    rear--;
   }
  }
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::DelFront()
  {
  
   size--;
   double shrMeasure;
   shrMeasure = capacity / 4.0;
   if (size &lt;= shrMeasure) // make an empty and shrink function
   {
    Shrink();
   }
  
   /*
   else if (front == rear)
   {
    if (front == 0)
     front = size - 1;
  
    else
    front++;
   }
   */
   else
   {
    if (front == size) //brings it full circle 
    {
     front = 0;
    }
  
    else
    {
     front++;
    }
   }
   if (front &gt; capacity)
    front = front % capacity;
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::Length()
  {
   return size;
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::Capacity()
  {
   return capacity;
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::Clear()
  {
   ~CDA();
   size = 1;
   circArray[size] = NULL;
  
  }
  
  template &lt;class T&gt;
  bool CDA&lt;T&gt;::Ordered()
  {
   return ordered;
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::SetOrdered()
  {
  
   for (int i = 1; i &lt; size - 1; i++)
   {
    if (circArray[(i - 1)] &gt; circArray[i])
    {
     ordered = false;
     return -1;
    }
   }
   ordered = true;
   return 1;
  
  }
  
  template &lt;class T&gt;
  T CDA&lt;T&gt;::Select(int k)
  {
   if (ordered == true)
   {
    return circArray[(front + k - 1) % capacity];
   }
   else
    QuickSelect(front, front + (size - 1), k);
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::QSelPartition(int left, int right)
  {
   int pivot = circArray[right % capacity];
   int x = left - 1;
  
   //Swap(&amp;circArray[pivIndex], &amp;circArray[right]);
  
   for (int i = left; i &lt;= right - 1; i++)
   {
    if (circArray[i % capacity] &lt;= pivot)
    {
     x++;
     Swap(&amp;circArray[x % capacity], &amp;circArray[i % capacity]);
    }
   }
   Swap(&amp;circArray[(x + 1) % capacity], &amp;circArray[right % capacity]);
  
   return (x + 1);
  }
  
  template &lt;class T&gt;
  T CDA&lt;T&gt;::QuickSelect(int left, int right, int k)
  {
   if (k &gt; 0 &amp;&amp; k &lt;= (right - left) + 1)
   {
    int index = QSelPartition(left, right);
  
    if (index - 1 == k - 1)
     return circArray[index % capacity];
  
    else if (index - 1 &gt; k - 1)
     return QuickSelect(left, index - 1, k);
  
    else
     return QuickSelect(index - 1, right, k - index + left - 1);
   }
   return -1;
  }
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::InsertionSort() //must be utilized in quicksort
  {
   for (int i = front + 1; i &lt; (front + size); i++)
   {
    int val = circArray[i % capacity];
    int inc = (i - 1) % capacity;
  
    while (inc &gt;= 0 &amp;&amp; circArray[inc] &gt; val)
    {
     circArray[(inc + 1) % capacity] = circArray[inc];
     inc--;
  
     if (inc == -1)
     {
      inc = capacity - 1;
     }
    }
    circArray[(inc + 1) % capacity] = val;
   }
   ordered = true;
  }
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::QuickSort() // change to other quicksort before leaving the ferg 
  {
   QuickSort1(front, front + (size - 1));
  }
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::QuickSort1(int low, int high)
  {
   while (low &lt; high)
   {
    if (high - low &lt; 900)
    {
     InsertionSort();
     break;
    }
    else
    {
     int pivot = QSortPartition(low, high);
  
     if (pivot - low &lt; high - pivot)
     {
      QuickSort1(low, pivot--);
      low = pivot + 1;
     }
     else
     {
      QuickSort1(pivot++, high);
      high = pivot - 1;
  
     }
    }
   }
  
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::QSortPartition(int low, int high)
  {
   int pivot = circArray[Median(low, high) % capacity];
   Swap(&amp;circArray[(Median(low, high)) % capacity], &amp;circArray[(high) % capacity]);
  
   int index = low % capacity;
  
   for (int i = low; i &lt; high; i++)
   {
    if (circArray[i % capacity] &lt;= pivot)
    {
     T t = circArray[i % capacity];
     circArray[i % capacity] = circArray[index % capacity];
     circArray[index % capacity] = t;
     index++;
    }
   }
   Swap(&amp;circArray[index % capacity], &amp;circArray[high % capacity]);
  
   return index;
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::Median(int low, int high)
  {
   T left, mid, right;
   left = circArray[low % capacity];
   mid = circArray[((low + high) / 2) % capacity];
   right = circArray[high % high];
  
   if (left &lt; right &amp;&amp; left &gt; mid)
    return low % capacity;
   if (left &lt; mid &amp;&amp; left &gt; right)
    return low % capacity;
   if (right &lt; left &amp;&amp; right &gt; mid)
    return high % capacity;
   if (right &lt; mid &amp;&amp; right &gt; left)
    return high % capacity;
   if (mid &lt; left &amp;&amp; mid &gt; right)
    return ((low + high) / 2 % capacity);
   if (mid &lt; right &amp;&amp; mid &gt; left)
    return ((low + high) / 2 % capacity);
  }
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::Swap(int* x, int* y)
  {
   int temp = *x;
   *x = *y;
   *y = temp;
  }
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::CountingSort(int m)  ////NEED TO FIX THIS 
  {
   int* OP = new int[size];
   int* Counter = new int[m + 1];
  
   for (int i = front; i &lt;= rear; i++)
   {
    cout &lt;&lt; "CircArray[" &lt;&lt; i &lt;&lt; "] is " &lt;&lt; circArray[i] &lt;&lt; endl;
   }
   for (int i = 0; i &lt;= m; i++)
   {
    Counter[i] = 0;
   }
   for (int i = front; i &lt; front + (size); i++)
   {
    Counter[circArray[i % capacity]]++;
   }
   for (int i = 1; i &lt;= m; i++)
   {
    Counter[i] += Counter[i - 1];
   }
  
   for (int i = rear - 1; i &gt; 0; i--)
   {
    OP[Counter[circArray[i]] - 1] = circArray[i];
    cout &lt;&lt; "Circular array at " &lt;&lt; i &lt;&lt; " is " &lt;&lt; circArray[i] &lt;&lt; endl;
    Counter[circArray[i]] -= 1;
    if (i == front % capacity)
     break;
  
    if (i == 0)
     i = capacity;
  
   }
  
   for (int i = 0; i &lt; size; i++)
    circArray[i] = OP[i];
  
   ordered = true;
   front = 0;
  }
  
  
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::Search(T e)
  {
   if (ordered == true) //binary search of item e
   {
    return BinarySearch(front, front + (size - 1), e);
   }
  
   else if (ordered == false)
   {
    for (int i = 0; i &lt; size - 1; i++)
    {
     if (circArray[i] == e)
      return i;
    }
   }
  
   return -1;
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::BinarySearch(int left, int right, T e)
  {
   while (left &lt;= right)
   {
    int mid = (left + right) / 2;
    int value = circArray[mid % capacity];
    if (value == e)
     return (mid - front) % capacity;
  
    else if (value &lt; e)
     return BinarySearch(mid + 1, right, e);
  
    else if (value &gt; e)
     return BinarySearch(left, mid - 1, e);
  
   }
  
   return -1;
  
  }
  
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::reSize()
  {
   capacity = capacity * 2;
   T *nArray = new T[capacity];
   for (int i = 0; i &lt; size - 1; i++)
   {
    int l = (front + i) % (size-1);
    nArray[i] = circArray[l];
   }
   //delete[]circArray;
   circArray = nArray;
   front = 0;
   rear = (size - 1);
  }
  
  
  template &lt;class T&gt;
  void CDA&lt;T&gt;::Shrink()
  {
   int tFront = front;
   capacity = capacity / 2;
   T* bArr = new T[capacity];
   int index = 0;
   while (front &lt;= rear)
   {
    bArr[index] = circArray[(front + index) % capacity];
    index++;
   }
   T* circArray = bArr;
   front = 0;
   rear = (size - 1);
  }
  
  template &lt;class T&gt;
  CDA&lt;T&gt;&amp; CDA&lt;T&gt;::operator=(const CDA&lt;T&gt;&amp; a)
  {
   if (this != &amp;a)
   {
    delete[]circArray;
    size = a.size;
    capacity = a.capacity;
    circArray = new T[a.capacity];
    front = a.front;
    rear = a.size - 1;
    ordered = a.ordered;
    for (int i = a.front; i &lt; a.front + (a.size); i++)
    {
     circArray[i % capacity] = a.circArray[i % capacity];
    }
   }
   return *this;
  }
  
  template &lt;class T&gt;
  int CDA&lt;T&gt;::Front()
  {
   return front;
  }
  
  template &lt;class T&gt;
  T CDA&lt;T&gt;::Data(int n)
  {
   return circArray[n];
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#include &lt;iostream&gt;
  #include "CDA-1.cpp"
  using namespace std;
  
  template&lt;class keytype, class valuetype&gt;
  class Heap
  {
  private:
   CDA&lt;keytype&gt;* K;
   CDA&lt;valuetype&gt;* V;
   int size;
  
  public: 
   Heap()
   {
    this-&gt;K = new CDA&lt;keytype&gt;();
    this-&gt;V = new CDA&lt;valuetype&gt;();
    size = 0;
   }
  
   Heap(keytype k[], valuetype v[], int s)
   {
    //allocate two different arrays for each type
    //fill those arrays concurrently using insert
    //sort concurrently using heapafy recursively
    this-&gt;K = new CDA&lt;keytype&gt;(s);
    this-&gt;V = new CDA&lt;valuetype&gt;(s);
    this-&gt;size = s;
    for (int i = 0; i &lt; s; i++)
    {
     insert(k[i], v[i]);
    }
    heapify(s, K-&gt;Front());
   }
  
   void heapify(int s, int i)
   {
    //Errors for evans to fix: swap the n's with s. 
           //Fix the left and right variable logic. Hepaify smallest not small at the bottom. 
           //Also we need to pass V[] in a parameter so we can edit it in this. 
  
    //How to better swap V with K and not just K.
    int smallest = i; 
    int left = 2*i +(-i+1);  
    int right = 2*i + (-i+2);
    
    keytype kl = K-&gt;Data(left);
    keytype kr = K-&gt;Data(right);
    keytype ks = K-&gt;Data(smallest);
       if (left &lt; s &amp;&amp; kl &lt; ks) //FIX 
     smallest = left;
  
    if (right &lt; s &amp;&amp; kr &lt; ks) //FIX
     smallest = right;
  
    if (smallest != i)
    {
     swap(K[i], K[smallest]);
     swap(V[i], V[smallest]); //FIX
  
     heapify(s, smallest);
    }
   }
  
   ~Heap() {
    return;
   }
  
   //items should be inserted using bottom up heap building method
   void insert(keytype k, valuetype v)
   {
    K-&gt;AddEnd(k);
    V-&gt;AddEnd(v);
    
    heapify(size, K-&gt;Front());
   }
  
  
   keytype peekKey()
   {
    int f = K-&gt;Front();
    return K-&gt;Data(f);
   }
  
   valuetype peekValue()
   {
    int f = V-&gt;front();
    return V-&gt;Data(f);
   }
  
   keytype extractMin()
   {
    keytype temp = K-&gt;Data(K-&gt;Front());
    K-&gt;DelFront();
    V-&gt;DelFront();
    heapify(size, K-&gt;Front());
    return temp;
   }
  
   void printKey()
   {
    ActualPrintKey(K-&gt;Front());
   }
  
   void ActualPrintKey(int n)
   {
    keytype rt = K-&gt;Data(n);
    if (rt != size)
    {
     cout &lt;&lt; K-&gt;Data(rt) &lt;&lt; " ";
     ActualPrintKey((2 * n) + (-n + 1));
     ActualPrintKey((2 * n) + (-n + 2)); 
    }
   }
  };
  
  /*
  template &lt;class keytype, class valuetype&gt;
  class BHeap
  {
   BHeap();
  
   BHeap(keytype k[], valuetype v[], int s);
  
   ~BHeap();
  
   keytype peekKey();
  
   valuetype peekValue();
  
   keytype extractMin();
  
   //items should be inserted using repeated insertion
   void insert(keytype k, valuetype v);
  
   void merge(BHeap&lt;keytype, valuetype&gt;&amp; H2);
  
   void printKey(); 
  };
  */</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#include &lt;iostream&gt;
  #include "Heaps.cpp"
  
  using namespace std;
  
  int main() {
   
   string K[10] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "K" };
   int V[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
  
   Heap&lt;string, int&gt; T1, T2(K, V, 10);
   cout &lt;&lt; T2.peekKey() &lt;&lt; endl;
   cout &lt;&lt; endl;
   system("pause");
  
   return 0;
  }</code></pre>
  </div>
  </div>
  </p>