title: Dynamic memory reallocation problem
body: <p>Can anyone tell me why this program is giving a debug error with message<br>
  "DAMAGE:after normal block(#42) at 0x00430050".
  The message is generated on line free(ptr);<br>
  I suppose that the problem is connected with the reallocation of memory. </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #define DELTA 5
  
  void include(int d,int* p,int n,int k,int flag);
  
  void main(void)
  {
   int *ptr;     
   int i=0,digit,koef=1;
  
   ptr=(int *)malloc(DELTA*sizeof(int));  
   fp=fopen("test.txt", "r")) 
   do{    
  
        fscanf(fp,"%d",&amp;digit);  
        if (!(i % DELTA))
          koef++;
        if(i&lt;(DELTA*koef))
          include(digit,ptr,i,koef,1);
        else
         include(digit,ptr,i,koef,2);
         i++;     
    }
  
  }while(!feof(fp));     
  
  free(ptr);      
  }
  
   void include(int d,int* p,int n,int k,int flag)
   {
      switch(flag){
          case 1: *(p+n)=d;break;
          case 2: if((p=(int *)realloc(p,k*DELTA*sizeof(int)))==NULL){  
                 printf("Error!Memory not allocated!\n");
                 exit(1);
   }
         *(p+n)=d;break;
   }
  }
  </code></pre>