Partage
  • Partager sur Facebook
  • Partager sur Twitter

valgrind invalid read

    30 octobre 2019 à 18:10:13

    Bonjour,

    Je fais quelques tests rapides pour mon projet (le but est de savoir si 2 mots sont des anagrammes) : en utilisant valgrind : j'ai cette erreur :

    ==8423== Memcheck, a memory error detector
    ==8423== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==8423== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
    ==8423== Command: ./test
    ==8423== 
    PARTIE 1
    
    string_are_anagrams : 
    Censé afficher : 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 
    i = 1
    i = 2
    i = 3
    i = 1
    i = 2
    i = 3
    i = 1
    i = 2
    i = 3
    i = 1
    i = 2
    i = 3
    i = 1
    i = 2
    i = 3
    ==8423== Invalid read of size 1
    ==8423==    at 0x1091DE: str_length.part.0 (stringslib.c:13)
    ==8423==    by 0x10A100: str_remove_char_with_const_char (anagrammes.c:21)
    ==8423==    by 0x10A306: string_are_anagrams (anagrammes.c:110)
    ==8423==    by 0x108A96: test_part1 (test.c:21)
    ==8423==    by 0x10893A: main (test.c:279)
    ==8423==  Address 0x522d483 is 0 bytes after a block of size 3 alloc'd
    ==8423==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==8423==    by 0x109B4A: str_duplicate (stringslib.c:284)
    ==8423==    by 0x10A2E7: string_are_anagrams (anagrammes.c:102)
    ==8423==    by 0x108A96: test_part1 (test.c:21)
    ==8423==    by 0x10893A: main (test.c:279)
    ==8423== 
    ==8423== Invalid read of size 1
    ==8423==    at 0x10A126: str_remove_char_with_const_char (anagrammes.c:27)
    ==8423==    by 0x10A306: string_are_anagrams (anagrammes.c:110)
    ==8423==    by 0x108A96: test_part1 (test.c:21)
    ==8423==    by 0x10893A: main (test.c:279)
    ==8423==  Address 0x522d483 is 0 bytes after a block of size 3 alloc'd
    ==8423==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==8423==    by 0x109B4A: str_duplicate (stringslib.c:284)
    ==8423==    by 0x10A2E7: string_are_anagrams (anagrammes.c:102)
    ==8423==    by 0x108A96: test_part1 (test.c:21)
    ==8423==    by 0x10893A: main (test.c:279)
    ==8423== 
    a = 3 
    i = 1
    i = 2
    a = 2 
    i = 1
    a = 1 
    1
    ==8423== 
    ==8423== HEAP SUMMARY:
    ==8423==     in use at exit: 0 bytes in 0 blocks
    ==8423==   total heap usage: 2 allocs, 2 frees, 1,027 bytes allocated
    ==8423== 
    ==8423== All heap blocks were freed -- no leaks are possible
    ==8423== 
    ==8423== For counts of detected and suppressed errors, rerun with: -v
    ==8423== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
    

    Voila ma fonction string_are_anagrams :

    bool string_are_anagrams(const char *str1, const char *str2) {
      size_t i = str_length(str1);
      size_t j = str_length(str2);
      const char *l;
      if(i != j){
        return false;
      }
      else{
        char *aux = str_duplicate(str2);
        for(size_t k = 0 ; k < i ; k++){
          l = str_search_first_char(aux,str1[k]);
          //printf("l = %ld\n\n\n",l);
          if(l == NULL){
            return false;
          }
          else{
            str_remove_char_with_const_char(aux,l);
          }
        }
        free(aux);
      }
      return true;
    }

    Voila ma fonction str_remove_char_with_const_char :

    void str_remove_char_with_const_char(char *str,const char *l){//l pointe sur le char qui à enlever
      size_t i = str_length(str);
      //printf("l = %ld\n",l);
      //printf("str = %ld\n",stsizeof(size_t)r);
      size_t a = l-str;
      //printf("a = %ld\n",a);
      //printf("str[0] = %c\n",str[0]);
      while(a != i){
        str[a] = str[a+1];
        a++;
      }
      printf("a = %ld \n",a);
      //free(str[a]);
    }

    ma fonction str_length :

    size_t str_length(const char *str) {
    	const char *str1 = "ab";
    	if(str == NULL){
    		return 0;
    	}
        size_t i = 0;
        while(str[i] != '\0'){
            i++;
    		printf("i = %ld\n",i);
        }
        return i;
    }


    et puis pour finir ma fonction _str_duplicate :

    char *str_duplicate(const char *str) {
    	int size = str_length(str);
    	size_t j = 0;
    	char *duplicate = (char *)calloc(str_length(str),sizeof(char));
    	while(j != size){
    		duplicate[j] = str[j];
    		if(j== size){
    			duplicate[j] = '\0';
    		}
    		j++;
    	}
    	return duplicate;
    }


    Je test toutes ces fonctions avec une seule ligne :

    printf("%d\n",string_are_anagrams("abc","abc"));


    Je ne comprends pas pourquoi Valgrind m'affiche ces erreurs quelqu'un aurait une idée ?
    Si je ne pouvais pas lire toute la chaine de const char * il me l'aurait dit dans le compilateur non ?

    -
    Edité par RomainGENDREAU1 30 octobre 2019 à 18:12:04

    • Partager sur Facebook
    • Partager sur Twitter

    valgrind invalid read

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown