Qui pourrait expliquer mon pb de core dump ?
Bonjour je me permet de poster un petit morceau de code pour illustrer un probleme de core dump que je n'explique pas.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| int main() {
char * string1 = (char *) malloc( 6 * sizeof(char) );
char * string2 = (char *) malloc( 6 * sizeof(char) );
char ** ptr = (char **) calloc(100, sizeof(char*)); // reserve 100 slots
for ( int idx = 0; idx < 100; ++idx )
printf("ptr[%d] = %x\n", idx, ptr[idx] );
sprintf( string1, "Hello");
//memcpy ( string2, "World", 6 ); // idem que le sprintf
sprintf ( string2, "World");
ptr[0]= &string1[3];
ptr[1]= &string2[1];
// print result
printf("<%s>\n", ptr[0] );
printf("<%s>\n", ptr[1] );
// here we overlap string1, but volontary over sized too
//memcpy(ptr[0], "tes laitues sechent-elles ?", 28 );
sprintf(ptr[0], "tes laitues sechent-elles ?" );
//memcpy(ptr[0], "hilll", 6 );
// print result
printf("<%s>\n", string1 );
printf("<%s>\n", ptr[0] );
// print
for ( int idx = 0; idx < 100; ++idx )
printf("ptr[%d] = %x\n", idx, ptr[idx] );
ptr[0] = 0;
ptr[1] = 0;
// print
for ( int idx = 0; idx < 100; ++idx )
printf("ptr[%d] = %x\n", idx, ptr[idx] );
free( ptr ); // core dump !
printf("End\n");
} |
il y a ecrasement memoire ici volontaire ici
Code:
sprintf(ptr[0], "tes laitues sechent-elles ?" );
si cette ligne est commentee alors tout est OK.
Mais vous pouvez voir que les pointeurs ptr[0] et ptr[1] sont forces a 0.
Donc j' aimerais savoir comment le programme plante sur le free() ! C est comme si il essayait de liberer la ressource de string1 alors qu il n a plus l'addresse pour y acceder.
On ne voit pas l affichage de "End".
Voici la pile d appel pour le core dump:
Code:
1 2 3 4 5 6 7 8
| (gdb) bt
#0 0xffffe410 in __kernel_vsyscall ()
#1 0xb7d36770 in raise () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7d37ef3 in abort () from /lib/tls/i686/cmov/libc.so.6
#3 0xb7d6bd0b in __fsetlocking () from /lib/tls/i686/cmov/libc.so.6
#4 0xb7d738bd in mallopt () from /lib/tls/i686/cmov/libc.so.6
#5 0xb7d73a44 in free () from /lib/tls/i686/cmov/libc.so.6
#6 0x0804883c in main () at test4.cpp:48 |