Bonjour,
Comme indiqué dans l'intitulé, j'essaye de récupérer un texte provenant d'un fichier ligne par ligne et stocker les lignes dans un tableau de char*.
La compilation s'effectue sans problème, mais voici ce que j'obtiens quand j'essaye d'exécuter :
1;2 //
2;3 // cela marche bien pour les deux premières lignes mais après :
*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001e682c0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7c8dc)[0x7fa1399ae8dc]
/lib64/libc.so.6(+0x87789)[0x7fa1399b9789]
/lib64/libc.so.6(cfree+0x16e)[0x7fa1399bf0ee]
./a.out[0x40081d]
./a.out[0x40088e]
/lib64/libc.so.6(__libc_start_main+0xea)[0x7fa13995250a]
./a.out[0x40067a] [...]
voici mon code si vous voulez bien jeter un coup d'oeil...
d'avance merci !
Valgrind me dit que le problème vient du malloc ligne 19
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 char **create_tmp(char *str, int line) { FILE *file; char **data; char *buf; size_t size; int i = 0; int j = 0; // is_empty(str); file = fopen(str, "r"); data = malloc(sizeof(char *) * (line + 1)); if (data = NULL) return(NULL); while (i < line) { buf = NULL; j = getline(&buf, &size, file); if ( j == -1) return(NULL); data[i] = malloc(sizeof(char) * (strlen(buf) + 1)); if (data[i] == NULL) return(NULL); strcpy(data[i], buf); data[i][strlen(buf) - 1] = '\0'; i++; } data[i] = NULL; fclose(file); return(data); } int main(int ac, char **av) { char **tmp = NULL; tmp = create_tmp(av[1], 13); }
PS : désolé je suis nouveau sur le forum ^^
Partager