title: HEAP CORRUPTION DETECTED: after Normal block (#195) at 0x00913A10. CRT detected that the application wrote to memory after end of heap buffer
body: <p>i have the following code on VS for writing a polynomial calculator class. 
  .h file is as below </p>
  
  <pre><code>#pragma once
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  using namespace std; 
  
  class Poly {
  private:
      int coefficient=0;
      int largest_exponent=0;
      int* array = nullptr;
      //string input;
  public:
      //constructor
      Poly(int coefficient, int largest_exponent);
      Poly(int coefficient); // assume the exponent or power is 0
      Poly(); // empty constructor 0 coeffecient and 0 exponent
      Poly(const Poly&amp; poly); //copy constructor
  
      //destructoer
      ~Poly();
      int getCoeff(int num);
      void setCoeff(int coeff, int exp);
      //delete and valgrind
  
      //Overload
  
      Poly operator*(const Poly &amp;poly) const;
      Poly operator+(const Poly &amp;poly) const;
      //Poly operator+(const Poly &amp;poly);
      Poly operator-(const Poly &amp;poly) const;
  
      Poly operator*=(const Poly &amp;poly) const;
      Poly operator+=(const Poly &amp;poly) const;
      Poly operator-=(const Poly &amp;poly) const;
  
      Poly operator=(const Poly &amp;poly) const;
      Poly operator==(const Poly &amp;poly) const;
      Poly operator!=(const Poly &amp;poly) const;
  
      friend ostream&amp; operator&lt;&lt;(ostream &amp;os, const Poly &amp;poly);
      friend istream&amp; operator&gt;&gt;(istream &amp;is, Poly &amp;poly);
  };
  </code></pre>
  
  <p>and my .cpp file is as below</p>
  
  <pre><code>#include "pch.h"
  #include "Poly.h"
  
  
  Poly::Poly(int coefficient, int largest_exponent) { // largest component must be +ve value 
      this-&gt;largest_exponent = largest_exponent;
      this-&gt;coefficient = coefficient;
  
      this-&gt;array = new int[largest_exponent+1];
      for (int i = 0; i &lt;= largest_exponent; i++) {
          this-&gt;array[i] = 0;    // Initialize all elements to zero.
      }
      this-&gt;array[largest_exponent] = coefficient;
  }
  
  Poly::Poly(int coefficient) { // assume the exponent or power is 0
      this-&gt;largest_exponent = 0;
      this-&gt;coefficient = coefficient;
  
      this-&gt;array = new int[1];
      for (int i = 0; i &lt;= 0; i++) {
          this-&gt;array[i] = 0;    // Initialize all elements to zero.
      }
      this-&gt;array[0] = coefficient;
  }
  
  Poly::Poly() { // empty constructor 0 coeffecient and 0 exponent
      this-&gt;coefficient = 0;
      this-&gt;largest_exponent = 0;
  
      //this-&gt;array = new int[1];
      this-&gt;array = new int[2];
      for (int i = 0; i &lt;= 0; i++) {
          this-&gt;array[i] = 0;    // Initialize all elements to zero.
      }   
  }
  
  Poly::Poly(const Poly&amp; poly) { //copy constructor
      this-&gt;coefficient = poly.coefficient;
      this-&gt;largest_exponent = poly.largest_exponent;
  
      this-&gt;array = new int[poly.largest_exponent+1];
      for (int i = 0; i &lt;= poly.largest_exponent; i++) {
          this-&gt;array[i] = poly.array[i];    // Initialize all elements to zero.
      }
  }
  
  Poly::~Poly() {
      cout &lt;&lt; "deleting Polynomial array" &lt;&lt; endl;
      delete[] array;
      array = nullptr; //or delete array;
  }
  
  int Poly::getCoeff(int num) {
      int coeff = this-&gt;array[num];
      if (coeff &gt; -33686019) {
          cout &lt;&lt; "coeff of power " &lt;&lt; num &lt;&lt; " of P is: " &lt;&lt; coeff &lt;&lt; endl;
          return this-&gt;array[num];
      }
      else {
          cout &lt;&lt; "this Exponent does not exist in the polynomial" &lt;&lt; endl;
          return 0;
      }
  }
  
  void Poly::setCoeff(int coeff, int exp) {
      int* newarray = new int[(abs(exp)) + 1];
  
      for (int i = 0; i &lt;= (abs(exp));i++) {
          newarray[i] = 0;
      }
  
      for (int i = 0; i &lt;= this-&gt;largest_exponent;i++) {
          newarray[i] = this-&gt;array[i];
      }
      newarray[(abs(exp))] = coeff;
      this-&gt;array = newarray;
      //this-&gt;array[abs(exp)] = coeff; 
      if ((abs(exp)) &gt; this-&gt;largest_exponent) {
          this-&gt;largest_exponent = (abs(exp));
      }
  }
  
  ostream&amp; operator&lt;&lt;(ostream &amp;os, const Poly &amp;poly) {
      bool flag = false;
  
      for (int i = poly.largest_exponent; i &gt;= 0; i--) {
              if (poly.array[i] &gt; 0 &amp;&amp; i &gt; 1) {
                  os &lt;&lt; " +" &lt;&lt; poly.array[i] &lt;&lt; "x^" &lt;&lt; i;
                  flag = true;
              }
              if (poly.array[i] &lt; 0 &amp;&amp; i &gt; 1) {
                  os &lt;&lt; " " &lt;&lt; poly.array[i] &lt;&lt; "x^" &lt;&lt; i;
                  flag = true;
              }
              if (poly.array[i] &gt; 0 &amp;&amp; i == 1) {
                  os &lt;&lt; " +" &lt;&lt; poly.array[i] &lt;&lt; "x";
                  flag = true;
              }
              if (poly.array[i] &lt; 0 &amp;&amp; i == 1) {
                  os &lt;&lt; " " &lt;&lt; poly.array[i] &lt;&lt; "x";
                  flag = true;
              }
              if (poly.array[i] &gt; 0 &amp;&amp; i == 0) {
                  os &lt;&lt; " +" &lt;&lt; poly.array[i];
                  flag = true;
              }
              if (poly.array[i] &lt; 0 &amp;&amp; i == 0) {
                  os &lt;&lt; " " &lt;&lt; poly.array[i];
                  flag = true;
              }
      }
      if (flag == false) {
          os &lt;&lt; " 0";
      }
      return os;
  }
  
  istream&amp; operator&gt;&gt;(istream &amp;is, Poly &amp;poly) {  
      poly.largest_exponent = 0;  
      int * temparray=new int[0]; 
      int coeff;
      int exp;
      cout &lt;&lt; "Enter terms for polynomial A.  Enter a coefficient " &lt;&lt; endl;
      if (std::cin &gt;&gt; coeff &gt;&gt; exp) {
          if (coeff != -1 &amp;&amp; exp != -1) {
              poly.array[exp] = coeff;
              if (poly.largest_exponent &lt; coeff)
                  poly.largest_exponent = coeff;
          }
  
      }
      return is;
  }
  
  Poly Poly::operator*(const Poly &amp;poly) const {
      Poly * temp = new Poly();
      temp-&gt;largest_exponent = poly.largest_exponent + this-&gt;largest_exponent;
  
      for (int k = 0; k &lt;= temp-&gt;largest_exponent; k++) {
          temp-&gt;array[k] = 0;    // Initialize all elements to zero.
      }
  
      for (int i = 0; i &lt;= this-&gt;largest_exponent; i++) {
          for (int j = 0; j &lt;= poly.largest_exponent; j++) {
              temp-&gt;array[i + j] += (this-&gt;array[i] * poly.array[j]);
          }
      }
      return *temp;
  }
  
  Poly Poly::operator+(const Poly &amp;poly) const {
      Poly * temp = new Poly();
      int smallest_exponent;
      if (this-&gt;largest_exponent &gt;= poly.largest_exponent) {
          temp-&gt;largest_exponent = this-&gt;largest_exponent;
          smallest_exponent = poly.largest_exponent;
          for (int i = 0; i &lt;= temp-&gt;largest_exponent; i++) {
              temp-&gt;array[i] = this-&gt;array[i];
          }
      }
      else {
          temp-&gt;largest_exponent = poly.largest_exponent;
          smallest_exponent = this-&gt;largest_exponent;
          for (int i = 0; i &lt;= temp-&gt;largest_exponent; i++) {
              temp-&gt;array[i] = poly.array[i];
          }
      }
  
      for (int i = 0; i &lt;= smallest_exponent; i++) {
  
          temp-&gt;array[i] = poly.array[i] + this-&gt;array[i];
      }
      return *temp;
  }
  
  Poly Poly::operator-(const Poly &amp;poly) const {
      Poly * temp = new Poly();
      int smallest_exponent;
  
      if (this-&gt;largest_exponent &gt;= poly.largest_exponent) {
          temp-&gt;largest_exponent = this-&gt;largest_exponent;
          smallest_exponent = poly.largest_exponent;
          for (int i = 0; i &lt;= temp-&gt;largest_exponent; i++) {
              temp-&gt;array[i] = this-&gt;array[i];
          }
      }
      else {
          temp-&gt;largest_exponent = poly.largest_exponent;
          smallest_exponent = this-&gt;largest_exponent;
          for (int i = 0; i &lt;= temp-&gt;largest_exponent; i++) {
              temp-&gt;array[i] = poly.array[i];
          }
      }
  
      for (int i = 0; i &lt;= smallest_exponent; i++) {
          temp-&gt;array[i] = this-&gt;array[i] - poly.array[i];
      }
  
      if (this-&gt;largest_exponent &lt; poly.largest_exponent) {
          for (int i = smallest_exponent+1; i &lt;= poly.largest_exponent; i++) {
              temp-&gt;array[i] = temp-&gt;array[i]*-1;
          }
      }
      return *temp;   
  }
  </code></pre>
  
  <p>and this is my driver .cpp</p>
  
  <pre><code>#include "pch.h"
  #include &lt;iostream&gt;
  #include "Poly.h"
  
  int main()
  {
      Poly A(-5,1);
      int x = A.getCoeff(1);
      A.setCoeff(7,3);
      A.setCoeff(6,2);
      Poly B(2);
      B.setCoeff(8,2);
      B.setCoeff(8,0);
      Poly C; 
      Poly D(A);
      cout &lt;&lt; A &lt;&lt; endl;
      cout &lt;&lt; B &lt;&lt; endl;
      cout &lt;&lt; C &lt;&lt; endl;
      cout &lt;&lt; D &lt;&lt; endl;
      Poly E = B + A;
      cout &lt;&lt; E &lt;&lt; endl;
      Poly F = A-B;
      cout &lt;&lt; F &lt;&lt; endl;
      Poly G = B-A;   
      cout &lt;&lt; G &lt;&lt; endl;
      Poly H = B * A;
      cout &lt;&lt; H &lt;&lt; endl;
      cout &lt;&lt; endl;
      //Poly I;
      //cin &gt;&gt; I;
      //cout &lt;&lt; I &lt;&lt; endl;
      return 0;
  
  
  }
  </code></pre>
  
  <p>Whenever itry to run this code, i get the below error even though when i click continue the results seem to be fine. Anyone has an idea where am i messing up the memory? </p>
  
  <p>Debug Error!</p>
  
  <p>Program: </p>
  
  <p>HEAP CORRUPTION DETECTED: after Normal block (#195) at 0x00A03A10.
  CRT detected that the application wrote to memory after end of heap buffer.</p>
  
  <p>(Press Retry to debug the application)
  Critical error detected c0000374
  Poly.exe has triggered a breakpoint.</p>
  
  <p>Exception thrown at 0x77DC61E4 (ntdll.dll) in Poly.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77DE2378).
  Unhandled exception at 0x77DC61E4 (ntdll.dll) in Poly.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77DE2378).</p>
  
  <p>'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\user32.dll'. Cannot find or open the PDB file.
  'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
  'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
  'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
  'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file.
  'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\combase.dll'. Cannot find or open the PDB file.
  'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\dwmapi.dll'. Cannot find or open the PDB file.
  'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\ole32.dll'. Cannot find or open the PDB file.
  'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file.
  Exception thrown at 0x77D21796 (ntdll.dll) in Poly.exe: 0xC0000005: Access violation reading location 0x00000000.</p>