title: Need help resizing dynamic array - Heap corruption detected
body: <p>I am working on an assignment for my c++ class and am having trouble with one portion of the class that requires resizing a dynamic array. For this assignment, I need to store terms of a polynomial in a dynamic array. I am specifically having trouble with the setCoefficient function of my code, where I need to resize a dynamic array if I am trying to at a term to my polynomial that is larger than what the array can currently hold.</p>
  <p>I tried following some other stack overflow posts on how to resize a dynamic array, but keep running into errors. Currently, I am running into an error of HEAP CORRUPTION DETECTED when I try and delete the old dynamic array in the setCoefficient function.</p>
  <p>I have commented the setCoefficient function and was hoping someone could go through and help me determine where I am going wrong. Unfortunately, I am not allowed to use the STL vector class for this assignment.</p>
  <p><strong>Here is my .cpp file where you can see the implementation of the setCoefficient function that I am having trouble with:</strong></p>
  <pre><code>#include &quot;poly.h&quot;
  
  //Default constructor. Initializes a Poly to: 0x^0
  Poly::Poly()
  {
      cout &lt;&lt; &quot;No parameter constructor called&quot; &lt;&lt; endl;
      polyArray = new int[1];
      //polyArray[1] = 0;
      polyArray[0] = 0;
      length = 0;
  }
  
  //Constructor with one argument. Initialized to: coefficientx^0
  Poly::Poly(int coefficient)
  {
      cout &lt;&lt; &quot;One parameter constructor called&quot; &lt;&lt; endl;
      cout &lt;&lt; &quot;coefficient: &quot; &lt;&lt; coefficient &lt;&lt; endl;
      polyArray = new int[1];
      //polyArray[1] = 0;
      polyArray[0] = coefficient;
      length = 0;
  }
  
  //Constructor with two arguments. Initialized to: coefficientx^power
  Poly::Poly(int coefficient, int power)
  {
      cout &lt;&lt; &quot;Two parameter constructor called&quot; &lt;&lt; endl;
      cout &lt;&lt; &quot;power: &quot; &lt;&lt; power &lt;&lt; endl;
      cout &lt;&lt; &quot;coeff: &quot; &lt;&lt; coefficient &lt;&lt; endl;
      polyArray = new int[power]; //I think I need to add 1 here to account for that fact that arrays start at index 0.
      
      for (int i = 0; i &lt; power; i++) { //I want to initialize the dynamic array to all 0's
          polyArray[i] = 0;
      }
  
      polyArray[power] = coefficient;
      length = power;
  }
  
  //Destructor
  Poly::~Poly()
  {
      cout &lt;&lt; &quot;I am the destructor&quot; &lt;&lt; endl;
      polyArray = NULL;
      delete [] polyArray;
  }
  
  //Copy constructor. Creates a deep copy of a Poly.
  Poly::Poly(const Poly&amp; p)
  {
      cout &lt;&lt; &quot;Copy constructor&quot; &lt;&lt; endl;
      this-&gt;length = p.length;
      cout &lt;&lt; &quot;this-&gt;length: &quot; &lt;&lt; this-&gt;length &lt;&lt; endl;
      cout &lt;&lt; &quot;p.length: &quot; &lt;&lt; p.length &lt;&lt; endl;
  
      this-&gt;polyArray = new int[this-&gt;length];
  
      //Loop throguh the initial Poly and assign all values in the array to the new Poly.
      for (int i = 0; i&lt;=p.length; i++) {
          this-&gt;polyArray[i] = p.polyArray[i];
          cout &lt;&lt; &quot;this polyArray: &quot; &lt;&lt; this-&gt;polyArray[i] &lt;&lt; endl;
          cout &lt;&lt; &quot;p.polyArray: &quot; &lt;&lt; p.polyArray[i] &lt;&lt; endl;
      }
  }
  
  void Poly::setCoefficient(const int coefficient, const int power) {
      if (power &gt; this-&gt;length) {
          //we need to resize the array we currently have.
  
          //Here I am creating a new array that will temporarily store larger values
          int* resizedArray = new int[power * 2];
  
          //This for loop assigns all the values in the current object's array to the new array (resizedArray)
          for (int i = 0; i &lt; this-&gt;length; i++) {
              resizedArray[i] = this-&gt;polyArray[i];
          }
  
          //Here I am adding the term that we wanted to add in the first place with setCoefficient
          resizedArray[power] = coefficient;
  
          //Deleting the terms in polyArray
          delete [] this-&gt;polyArray;
          
          //Creating a new array that has been resized
          this-&gt;polyArray = new int[power * 2];
          
          //Setting the values of the temporary array, resizedArray, to the array of the current object.
          this-&gt;polyArray = resizedArray;
          
          //modifying the length of the current object.
          this-&gt;length = power * 2;
      }
      else {
          this-&gt;polyArray[power] = coefficient;
      }
  }
  </code></pre>
  <p><strong>and for completeness, here is my .h file as well (although I dont think the issue is in my .h:</strong></p>
  <pre><code>// ------------------------------------------------ Poly.h ----------------------------------------------
  #ifndef Poly_H                                  
  #define Poly_H
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class Poly {
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Poly&amp; p);
      friend istream&amp; operator&gt;&gt;(istream&amp; in, Poly&amp; p);
  
  public:
      //Member functions
      Poly(); //Default constructor. Initializes a Poly to: 0x^0
      Poly(int coefficient); //Constructor with one argument. Initialized to: coefficientx^0
      Poly(int coefficient, int power); //Constructor with two arguments. Initialized to: coefficientx^power
      Poly(const Poly&amp; p); //Copy constructor. Creates a deep copy of a Poly.
      ~Poly(); //Destructor
  
  
      int degree() const; //Returns the largest degree term in a Poly.
      double getCoefficient(const int power) const; //returns the coefficient of the x^power term.
      //Poly setCoefficient(const int coefficient, const int power); //Sets the coefficient of the term indicated by the power.
      void setCoefficient(const int coefficient, const int power);
  
  
      // Arithmetic operators
      Poly operator+(const Poly&amp; p) const;
      Poly operator-(const Poly&amp; p) const;
  
      // Boolean comparison operators
      bool operator==(const Poly&amp; p) const;
      bool operator!=(const Poly&amp; p) const;
  
      // Assignment operators
      Poly&amp; operator=(const Poly&amp; p);
  
      Poly&amp; operator+=(const Poly&amp; p);
      Poly&amp; operator-=(const Poly&amp; p);
      Poly&amp; operator*=(const Poly&amp; p);
  
      //Clean up/delete all Poly terms.
      void clear();
      //Checks if the Poly is empty or not.
      bool isEmpty() const;
  
  private:
      int length; //# of terms in the Poly/size of the array
      int* polyArray;
  };
  
  #endif
  
  </code></pre>