开发者

Dynamic memory reallocation problem

开发者 https://www.devze.com 2022-12-18 22:28 出处:网络
Can anyone tell me why this program is giving a debug error with message \"DAMAGE:after normal block(#42) at 0x00430050\".

Can anyone tell me why this program is giving a debug error with message

"DAMAGE:after normal block(#42) at 0x00430050". The message is generated on line free(ptr);

I suppose that the problem is connected with the reallocation of memory.

#include<stdio.h>
#include<stdlib.h>
#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",&digit);  
      if (!(i % DELTA))
        koef++;
      if(i<(DELTA*koef))
        include(digit,ptr,i,koef,1);
      else
       include(digit,ptr,i,koef,2);
       i++;     
  }

}while(!feof(fp));     

free(ptr);      
}
开发者_JS百科
 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;
 }
}


You are passing ptr into you function and then changing it using realloc. You need to pass a pointer to a pointer in order to make this work properly.

I suggest you print out the address that the pointer is pointing to before and after the call to include and before and after the call to realloc. This should show you what is happening or look at the code below:

#include <stdio.h>

void a( int **ptrptr)
{
    printf("ptrptr = %p,  ptr = %p\n",ptrptr,*ptrptr) ;
    *ptrptr = (int*)realloc(*ptrptr, 10*sizeof(int)) ;
    printf("ptrptr = %p,  ptr = %p\n",ptrptr,*ptrptr) ;
}


int main(int argc, char **argv)
{
    int *ptr = malloc(5*sizeof(int)) ;
    printf("ptr = %p\n",ptr) ;
    a(&ptr) ;
    printf("ptr = %p\n",ptr) ;
    free(ptr) ;
    return 0;
}


The realloc() call in include() may move the block. That's why realloc() returns a pointer, to tell you where it finally put the data. Your code temporarily uses that value (you store it in the local variable p and use it) but fails to propagate it back to the caller. The main() function keeps in its ptr local variable a pointer to the original block, and it never changes.

Remember that when you pass an argument to a function, the function gets its own copy. Here, you pass the contents of ptr as an argument, and include() sees that value as a variable named p, but this p is only a copy of ptr. Modifications to p are not seen by ptr.

You could modify include() so that it returns the new pointer; something like:

int *include(int d,int* p,int n,int k,int flag)
{
    ...
    return p;
}

and then call it with:

ptr = include(digit, ptr, i, koef, 1);

which is a way to propagate back the new pointer value to the caller.

Side notes:

  • main() should return an int, not void.
  • You do not have to cast the return value of malloc() or realloc(). These functions return a void * which the C compiler happily converts to any kind of pointer without an explicit cast. An explicit cast is a way to tell the compiler: "shut up, I know what I am doing". Since in that situation the compiler would not have talked anyway, the cast is just a waste of source code space. Also, the cast could be harmful in some situations where the compiler would have warned loudly: if malloc() was mispelled, the compiler would assume that it returns an int, and would warn about using an int as a pointer; but the cast would prevent that warning.
  • Your code does not compile, there is an extra closing brace.


Thanks a lot! Your remarks were realy helpful. The problem was in the logic of function include() calling. I corrected it and at the moment the program is working fine. My function is now returning pointer to the allocated memory block.

0

精彩评论

暂无评论...
验证码 换一张
取 消