j'ecrivais un code simple C pour tester une allocation de mémoire et pointeurs:Valgrind montre plus la mémoire allouée que ce qui est en fait
#include <stdlib.h>
#include <stdio.h>
int *randomAlloc(int n) {
int *address = NULL, i = 0;
address = malloc (n * sizeof(int));
for (i = 0; i < n ; i++){
*(address + i) = i ;
}
return address;
}
int main(int argc, char* argv[]) {
int *address;
int n;
printf("Type vector size: ");
scanf("%d", &n);
address = randomAlloc(n);
free(address);
}
Pourtant, pour une raison quelconque quand je type 4 sorties valgrind d'entrée:
==2375== Memcheck, a memory error detector
==2375== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==2375== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==2375== Command: ./a.out
==2375==
Type vector size: 4
==2375==
==2375== HEAP SUMMARY:
==2375== in use at exit: 0 bytes in 0 blocks
==2375== total heap usage: 3 allocs, 3 frees, 2,064 bytes allocated
==2375==
==2375== All heap blocks were freed -- no leaks are possible
==2375==
==2375== For counts of detected and suppressed errors, rerun with: -v
==2375== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Il y a seulement un alloc et un gratuit au code. Comme n = 4, je m'attendrais à allouer 4 * 4 (sizeof (int)) = 16 octets. D'où vient-il?
Tapez '1' ou' 2' ... ce sera toujours le même. La seule chose qui va changer est '2064 octets alloués'. Maintenant, jetez un coup d'oeil et réfléchissez, pourquoi. – Michi
Je ne peux pas reproduire le comportement que vous décrivez. Quand je construis ton code et que je l'exécute sous Valgrind, il rapporte une allocation et une gratuite, comme prévu. –
@JohnBollinger Pas sur mon système (linux mint). si je tape '2' j'ai:' l'utilisation totale de tas: 3 allocs, 3 libère, 2 056 octets alloués' si je tape '3' j'ai: 'utilisation totale de tas: 3 allocs, 3 libère, 2 060 octets alloués'. ... etc. '2,056' =>' 2,060' => '2064' – Michi