[C] problème avec free

Salut :hello:
J’obtiens l’erreur suivante :

*** glibc detected *** free(): invalid next size (fast): 0x0804a058 ***

avec le code ci-dessous :


int test3 (int* var, int n){
  int *presentes;
  int i,j;

  printf("malloc\n");
  presentes=(int*)malloc(var[n-1]*sizeof(int));
  for (i=0;i<var[n-1];i++)
    presentes[i]=0;

  for (i=0;i<n;i++)
    for (j=0;j<i;j++){
      if (presentes[var[i]-var[j]]==1){
	printf("free\n");
	free(presentes);
	printf("fin du free\n");
	return 0;
      }
      else
	presentes[(var[i]-var[j])]=1;
    }
  printf("free\n");
  free(presentes);
  printf("fin du free\n");
  return 1;
}

plus exactement l’éxécution donne :


malloc
free
fin du free
malloc
free
*** glibc detected *** free(): invalid next size (fast): 0x0804a058 ***
Abandon

En fait la fonction est appelée plusieurs fois. Ici ca plante au bout du deuxieme appel.

Quelqu’un pourrait m’expliquer ca ? :’(

Pas de plantage sous win32 (devc++).

tente un valgrind?

Avec les valeurs de var :


Var ==> 0 1 2
malloc de 2
free
fin du free
Var ==> 0 1 3
malloc de 3
free
*** glibc detected *** free(): invalid next size (fast): 0x0804a058 ***

J’espère que c’est pas un problème de ma glibc …

Je vais essayer valgrind voir ce qu’il raconte

EDIT : ca passe en lancant avec valgrind. Il doit "récupérer" des erreurs :heink:

Si ca peut vous aider :


ben81@Megator:~/Prog$ valgrind -v --leak-check=full --show-reachable=yes ./golomb 3
==11416== Memcheck, a memory error detector.
==11416== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==11416== Using LibVEX rev 1471, a library for dynamic binary translation.
==11416== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==11416== Using valgrind-3.1.0-Debian, a dynamic binary instrumentation framework.
==11416== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==11416==
--11416-- Command line
--11416--    ./golomb
--11416--    3
--11416-- Startup, with flags:
--11416--    -v
--11416--    --leak-check=full
--11416--    --show-reachable=yes
--11416-- Contents of /proc/version:
--11416--   Linux version 2.6.15-27-k7 (buildd@terranova) (gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu5)) #1 SMP PREEMPT Sat Sep 16 02:35:20 UTC 2006
--11416-- Arch and subarch: X86, x86-sse2
--11416-- Valgrind library directory: /usr/lib/valgrind
--11416-- Reading syms from /lib/ld-2.3.6.so (0x4000000)
--11416-- Reading debug info from /lib/ld-2.3.6.so...
--11416-- ... CRC mismatch (computed F305999E wanted 041122DD)
--11416--    object doesn't have a symbol table
--11416-- Reading syms from /home/ben81/Prog/golomb (0x8048000)
--11416-- Reading syms from /usr/lib/valgrind/x86-linux/memcheck (0xA000000)
--11416--    object doesn't have a dynamic symbol table
--11416-- Reading suppressions file: /usr/lib/valgrind/default.supp
--11416-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_core.so (0x4019000)
--11416-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so (0x401B000)
--11416-- Reading syms from /lib/tls/i686/cmov/libc-2.3.6.so (0x4035000)
--11416-- Reading debug info from /lib/tls/i686/cmov/libc-2.3.6.so...
--11416-- ... CRC mismatch (computed 2A207A12 wanted C99D524A)
--11416--    object doesn't have a symbol table
--11416-- REDIR: 0x409F640 (rindex) redirected to 0x401DD72 (rindex)
--11416-- REDIR: 0x409A390 (malloc) redirected to 0x401C3A0 (malloc)
--11416-- REDIR: 0x409F270 (strlen) redirected to 0x401DEE5 (strlen)
avant test
Var ==> 0 1 2
malloc de 2
==11416== Invalid read of size 4
==11416==    at 0x80486F1: test3 (golomb.c:60)
==11416==    by 0x80488E8: boucle (golomb.c:91)
==11416==    by 0x80489D8: main (golomb.c:134)
==11416==  Address 0x4165170 is 0 bytes after a block of size 8 alloc'd
==11416==    at 0x401C422: malloc (vg_replace_malloc.c:149)
==11416==    by 0x80486A8: test3 (golomb.c:54)
==11416==    by 0x80488E8: boucle (golomb.c:91)
==11416==    by 0x80489D8: main (golomb.c:134)
==11416==
==11416== Invalid write of size 4
==11416==    at 0x8048703: test3 (golomb.c:67)
==11416==    by 0x80488E8: boucle (golomb.c:91)
==11416==    by 0x80489D8: main (golomb.c:134)
==11416==  Address 0x4165170 is 0 bytes after a block of size 8 alloc'd
==11416==    at 0x401C422: malloc (vg_replace_malloc.c:149)
==11416==    by 0x80486A8: test3 (golomb.c:54)
==11416==    by 0x80488E8: boucle (golomb.c:91)
==11416==    by 0x80489D8: main (golomb.c:134)
free
--11416-- REDIR: 0x40982C0 (free) redirected to 0x401CF51 (free)
fin du free
apres test
Taille= 3
avant test
Var ==> 0 1 3
malloc de 3
free
fin du free
Var ==> 0 1 3
apres test
avant test
Var ==> 0 2 3
malloc de 3
free
fin du free
Var ==> 0 2 3
apres test
--11416-- REDIR: 0x40A0490 (memset) redirected to 0x401E209 (memset)
==11416==
==11416== ERROR SUMMARY: 6 errors from 2 contexts (suppressed: 11 from 1)
==11416==
==11416== 3 errors in context 1 of 2:
==11416== Invalid write of size 4
==11416==    at 0x8048703: test3 (golomb.c:67)
==11416==    by 0x80488E8: boucle (golomb.c:91)
==11416==    by 0x80489D8: main (golomb.c:134)
==11416==  Address 0x4165170 is 0 bytes after a block of size 8 alloc'd
==11416==    at 0x401C422: malloc (vg_replace_malloc.c:149)
==11416==    by 0x80486A8: test3 (golomb.c:54)
==11416==    by 0x80488E8: boucle (golomb.c:91)
==11416==    by 0x80489D8: main (golomb.c:134)
==11416==
==11416== 3 errors in context 2 of 2:
==11416== Invalid read of size 4
==11416==    at 0x80486F1: test3 (golomb.c:60)
==11416==    by 0x80488E8: boucle (golomb.c:91)
==11416==    by 0x80489D8: main (golomb.c:134)
==11416==  Address 0x4165170 is 0 bytes after a block of size 8 alloc'd
==11416==    at 0x401C422: malloc (vg_replace_malloc.c:149)
==11416==    by 0x80486A8: test3 (golomb.c:54)
==11416==    by 0x80488E8: boucle (golomb.c:91)
==11416==    by 0x80489D8: main (golomb.c:134)
--11416--
--11416-- supp:   11 Debian libc6 2.3.6 stripped dynamic linker
==11416==
==11416== IN SUMMARY: 6 errors from 2 contexts (suppressed: 11 from 1)
==11416==
==11416== malloc/free: in use at exit: 60 bytes in 5 blocks.
==11416== malloc/free: 8 allocs, 3 frees, 92 bytes allocated.
==11416==
==11416== searching for pointers to 5 not-freed blocks.
==11416== checked 80,020 bytes.
==11416==
==11416==
==11416== 12 bytes in 1 blocks are definitely lost in loss record 1 of 3
==11416==    at 0x401C422: malloc (vg_replace_malloc.c:149)
==11416==    by 0x80489B3: main (golomb.c:128)
==11416==
==11416==
==11416== 48 (12 direct, 36 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 3
==11416==    at 0x401C422: malloc (vg_replace_malloc.c:149)
==11416==    by 0x80489BE: main (golomb.c:129)
==11416==
==11416==
==11416== 36 bytes in 3 blocks are indirectly lost in loss record 3 of 3
==11416==    at 0x401C422: malloc (vg_replace_malloc.c:149)
==11416==    by 0x80489F7: main (golomb.c:131)
==11416==
==11416== LEAK SUMMARY:
==11416==    definitely lost: 24 bytes in 2 blocks.
==11416==    indirectly lost: 36 bytes in 3 blocks.
==11416==      possibly lost: 0 bytes in 0 blocks.
==11416==    still reachable: 0 bytes in 0 blocks.
==11416==         suppressed: 0 bytes in 0 blocks.
--11416--  memcheck: sanity checks: 0 cheap, 1 expensive
--11416--  memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--11416--  memcheck: auxmaps: 0 searches, 0 comparisons
--11416--  memcheck: secondaries: 8 issued (512k, 0M)
--11416--  memcheck: secondaries: 18 accessible and distinguished (1152k, 1M)
--11416--     tt/tc: 3,691 tt lookups requiring 3,724 probes
--11416--     tt/tc: 3,691 fast-cache updates, 2 flushes
--11416-- translate: new        1,839 (38,120 -> 623,984; ratio 163:10) [0 scs]--11416-- translate: dumped     0 (0 -> ??)
--11416-- translate: discarded  0 (0 -> ??)
--11416-- scheduler: 32,946 jumps (bb entries).
--11416-- scheduler: 0/2,020 major/minor sched events.
--11416--    sanity: 1 cheap, 1 expensive checks.
--11416--    exectx: 30,011 lists, 14 contexts (avg 0 per list)
--11416--    exectx: 28 searches, 14 full compares (500 per 1000)
--11416--    exectx: 3 cmp2, 35 cmp4, 0 cmpAll

J’ai testé tes valeurs avec succès.

EDIT : montre comment tu appels ta fonction.

if (test3(var,n)==1)

Je vais pas tarder à aller dormir (dure journée), je regarderai demain matin
merci en tout cas

Comment tu initialises var et n ?


int n;
int *var;

n=atoi(argv[1]);
var=(int*)malloc(n*sizeof(int));

Je dors ca y est :ane:

Ca marche la aussi :neutre:
Bonne nuit :sleep:

Pas beau atoi, strtol !

Ca donne quoi les lignes donnés par valgrind?

Je les ai mise plus haut.
Bon je vais finir par croire que ca vient de ma glibc :grrr:
Je vais aussi tester ce que renvoie malloc