title: Heap Corruption detected after Normal Block (#193) at 0x014E5A28
body: <p>I was doing a code in which I have to create a dynamic array and a pointer (the pointer should be used to move through the data of the array). I have to make methods get (to return the index of a number to be searched in the array), update (when get method returns the index the update method can update the value), length(this method should return the size of the array), next(this method should move the pointer to the next index of the array), start(this method should move the pointer to the start of the array), end(this method should move pointer to the end of array) and Add(it should add a block to the array)</p>
  
  <p>Here is the code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;conio.h&gt;
  
  using namespace std;
  
  class Runner
  {
  private:
      int *arr;
      int *ptr;
      int size;
  
      // to input in array
  
      void Input()
      {
          for (int i = 0; i &lt; size; i++,*ptr++)
          {
              cout &lt;&lt; "Enter value no. " &lt;&lt; i + 1 &lt;&lt; "\t";
              cin &gt;&gt; arr[i];
          }
      }
  
      //to allocate memory
      void Create()
      {
          cout &lt;&lt; "Enter the total number of elements :\t";
          cin &gt;&gt; size;
  
          arr = new int[size];
          ptr = arr;
  
          Input();
      }
  
  public:
      //constructor to define starting value
      Runner()
      {
          ptr = NULL;
          size = 0;
          arr = NULL;
      }
  
      //get function
      int get(int num)
      {
          bool NotNull = false;
          int index = 0;
  
          do
          {
              if (ptr != NULL)
              {
                  ptr = &amp;arr[0];
                  for (int i = 0; i &lt; size; i++,*(ptr++))
                  {
                      if (num == *ptr)
                      {
                          index = i;
                          break;
                      }
                  }
                  NotNull = true;
              }
  
              else
              {
                  Create();
              }
          } while (!NotNull);
  
          return index;
      }
  
      //update function
      void Update(int num)
      {
          int val;
          int index = get(num);
  
          cout &lt;&lt; "Enter value to update :\t";
          cin &gt;&gt; val;
  
          ptr = ptr + index;
          *ptr = val;
      }
  
      //length function
      int length()
      {
          if (ptr == NULL)
          {
              Create();
          }
  
          return size;
      }
  
      //moving pointer back
      void Back()
      {
          bool NotNull = false;
  
          do
          {
              if (ptr != NULL)
              {
                  *(ptr--);
                  NotNull = true;
              }
  
              else
              {
                  Create();
              }
          } while (!NotNull);
      }
  
      //moving pointer forward;
      void Next()
      {
          bool NotNull = false;
  
          do
          {
              if (ptr != NULL)
              {
                  *(ptr++);
                  NotNull = true;
              }
  
              else
              {
                  Create();
              }
          } while (!NotNull);
      }
  
      //to point at the start of array
      void Start()
      {
          bool NotNull = false;
  
          do
          {
              if (ptr != NULL)
              {
                  ptr = &amp;arr[0];
                  NotNull = true;
              }
  
              else
              {
                  Create();
              }
          } while (!NotNull);
      }
  
      //to move pointer to the end
      void End()
      {
          bool NotNull = false;
  
          do
          {
              if (ptr != NULL)
              {
                  ptr = &amp;arr[size - 1];
                  NotNull = true;
              }
  
              else
              {
                  Create();
              }
          } while (!NotNull);
      }
  
      //to add a block into array
      void Add()
      {
          int NewSize = size + 1;
          int *temp = new int[NewSize];
          for (int i = 0; i &lt; size; i++)
          {
              temp[i] = arr[i];
          }
          size = NewSize;
          ptr = temp;
          if (arr != NULL)
          {
              delete[] arr;
          }
          arr = NULL;
          arr = temp;
          cout &lt;&lt; "You have been provided a new block to add 1 data entry\n";
          cout &lt;&lt; "Enter value :\t";
          cin &gt;&gt; arr[size - 1];
      }
  };
  
  int main()  //main made just for testing above method's working
  {
      Runner *obj = new Runner();
  
      obj-&gt;get(3);
      obj-&gt;Update(20);
      cout &lt;&lt; obj-&gt;length() &lt;&lt; "\n";
      obj-&gt;Next();
      obj-&gt;End();
      obj-&gt;Back();
      obj-&gt;Start();
      obj-&gt;Add();
  
      delete obj;
  
      _getch();
      return 0;
  }
  </code></pre>
  
  <p>Well the part of code giving me error is the Add method in which i have to increase the size of array without using <code>realloc()</code>.</p>
  
  <p>The Add Method is as follows:</p>
  
  <pre><code>void Add()
      {
          int NewSize = size + 1;
          int *temp = new int[NewSize];
          for (int i = 0; i &lt; size; i++)
          {
              temp[i] = arr[i];
          }
          size = NewSize;
          ptr = temp;
          if (arr != NULL)
          {
              delete[] arr;   //the point where heap corruption error occurred
          }
          arr = NULL;
          arr = temp;
          cout &lt;&lt; "You have been provided a new block to add 1 data entry\n";
          cout &lt;&lt; "Enter value :\t";
          cin &gt;&gt; arr[size - 1];
      }
  </code></pre>
  
  <p>Help me in solving this issue.</p>