Bonjour,
J'ai un problème d'allocation de mémoire que je n'arrive pas à résoudre pour ma variable string_p.
Je pense que mon realloc dans la fonction lecture() est mauvais, mais je ne vois pas comment faire.

Merci d'avance.

Code c : 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
int lecture( char *, char **, FILE * );
 
int main(int argc, char ** argv)
{
 
	FILE * stream_p = NULL;
	char * string_p = NULL;		// Mot P.
	unsigned int m = 0;			// Taille du mot P.
 
 
	m = lecture( "P", &string_p, stream_p );
 
	return EXIT_SUCCESS;
}
 
int
lecture( char * nom_fichier, char** string, FILE * stream )
{
	int c;
	unsigned int taille_mot = 0;
 
 	// ouverture en lecture du fichier
	stream = fopen( nom_fichier, "r" ) ;
 
	if(stream == NULL )
	{
		printf( "Impossible d'ouvrir le fichier %s \n", nom_fichier );
		exit( EXIT_FAILURE );
 	}
 	printf( ".............. Ouverture du fichier %s \n", nom_fichier );
 
	// lecture du fichier
	string[0] = malloc(sizeof(char*)*taille_mot);
 
	// attention la case 0 n'est pas utilisé. 
	while((c = fgetc(stream)) != EOF)
	{
		// Ignore les retours chariots
		if(c != '\n')
		{
			string[taille_mot + 1] = (char*)realloc(string[taille_mot + 1],sizeof(char) * ( taille_mot + 1 ) );
			(*string)[taille_mot + 1] = c ;
			printf("%c",(*string)[taille_mot+1]);
			taille_mot++ ;
		}
	}
		printf("\n");
 
	// fermeture du fichier
	if( fclose(stream) == EOF )
	{
		printf( "\nProbleme de fermeture du fichier\n" );
		exit( EXIT_FAILURE ) ;
	}
	printf( ".............. Fermeture du fichier\n\n" );
 
	return taille_mot ;
}