Many thanks to all you.
I must say that I was lucky. I had a program chashing when (after a lot of times) free() was used. Based on one comment of mats
Originally Posted by
matsp
Normally, it has a small amount of data (8-32 bytes) in the memory just before (lower address) than the memory it returns for you to use. This data contains for example the size or the end of the memory allocated. --
Mats
I Inspect the value of (say) p[k] (k=-1, -2 for 32, 64 bits implementation of gcc)
And just before the crash this value had changed as consecuence of another asignation, say q[ l ] = vq. With this information then I fixed my program.
I think that if the implementations of malloc() and free are known (the place for the administrative data are known) a tool for catching any unwished modification of that data could be designed.
One last favor, could you please send me the output of
Code:
#include <stdio.h>
#include <stdlib.h>
#define N 100
//K = -1 for 32 bits gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
//K = -2 for 64 bits gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
#define K -1
#define NL "\n"
int main(int argc, char *argv[])
{
int *p, n = N, k = K;
//int *q, *r;
int i;
for( i = 0; i < n; i++ )
{
p = malloc( sizeof( int )*i );
/* q = malloc( sizeof( int )*i );
r = malloc( sizeof( int )*i );*/
printf("\n%i %i", i, p[k]) ;
/* printf("\n%i %i", i, q[k]) ;
printf("\n%i %i", i, r[k]) ;*/
free( p );
/* free( r );
free( s );*/
}
printf("%s", NL );
return 0;
}
with and without uncomenting the indicated lines.
Thanks again.
Juan Carlos.