Problème pointeur de tableau de type char

bonsoir,

voici mon probleme, j’ai un pointeur de tableau de type char.
si j’ai moins de 4 elements pas de probleme au dela, j’ai un message d’erreur:

*** glibc detected *** ./test1: realloc(): invalid next size: 0x0804b028 ***
======= Backtrace: =========
/lib/i686/cmov/libc.so.6[0xb7e9b624]
/lib/i686/cmov/libc.so.6[0xb7e9f1a1]
/lib/i686/cmov/libc.so.6(realloc+0x106)[0xb7e9fb46]
./test1[0x8048729]
./test1[0x804897a]
/lib/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e43455]
./test1[0x80485a1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 03:01 3252290    /home/userdev/gtk/ocr_v1-2/test1
08049000-0804a000 rw-p 00000000 03:01 3252290    /home/userdev/gtk/ocr_v1-2/test1
0804a000-0806c000 rw-p 0804a000 00:00 0          [heap]
b7d00000-b7d21000 rw-p b7d00000 00:00 0 
b7d21000-b7e00000 ---p b7d21000 00:00 0 
b7e2c000-b7e2d000 rw-p b7e2c000 00:00 0 
b7e2d000-b7f82000 r-xp 00000000 03:01 4023102    /lib/i686/cmov/libc-2.7.so
b7f82000-b7f83000 r--p 00155000 03:01 4023102    /lib/i686/cmov/libc-2.7.so
b7f83000-b7f85000 rw-p 00156000 03:01 4023102    /lib/i686/cmov/libc-2.7.so
b7f85000-b7f88000 rw-p b7f85000 00:00 0 
b7f8e000-b7f9a000 r-xp 00000000 03:01 4014124    /lib/libgcc_s.so.1
b7f9a000-b7f9b000 rw-p 0000b000 03:01 4014124    /lib/libgcc_s.so.1
b7f9b000-b7f9e000 rw-p b7f9b000 00:00 0 
b7f9e000-b7f9f000 r-xp b7f9e000 00:00 0          [vdso]
b7f9f000-b7fb9000 r-xp 00000000 03:01 4014412    /lib/ld-2.7.so
b7fb9000-b7fbb000 rw-p 0001a000 03:01 4014412    /lib/ld-2.7.so
bffea000-bffff000 rw-p bffeb000 00:00 0          [stack]
Abandon

voici la fonction pour mon pointeur de tableau:
j’ai essayer plusieurs solutions trouvées sur le net pour realloc et malloc, rien de bon!


char** F_Listage_Fichiers_Traiter(char *V_CDT, int **RNDF);
char** F_Listage_Fichiers_Traiter(char *V_CDT, int **RNDF)
{
	/* parcourir un repertoire */
	DIR *dh;
	int i = 0;
	char **testtab = NULL;

	struct dirent *f;
	if ((dh = opendir (V_CDT)) == NULL)
		{
		/* erreur:A */
		printf("chemin & nom du dossier a traiter incorrecte! erreur:A\n");
		}
	else
		{
		char *extens;
		char *reffext = "txt";
		do			
			{
			if (NULL != (f = readdir (dh)))
				{
				if (strcmp(f->d_name,".") && strcmp(f->d_name, "..") && f->d_name != NULL)
					{
					extens = strstr(f->d_name, "txt");
					if (extens != NULL)
						{
						if (strcmp(extens, reffext) == 0)
							{
// a voir ?	
		testtab = realloc(testtab, sizeof(char) * i + 1);
				//		testtab = (char**)realloc(testtab, sizeof(char) * i + 1);
						//	testtab = (char**)realloc(testtab, sizeof(char*) * i);
							if (testtab == NULL)
								{
									printf ("probleme A etape : %d\n", i);
								}
// a voir ?
		testtab[i] = malloc(sizeof(char) * strlen(f->d_name) +1);
				//	testtab[i] = (char*)malloc(sizeof(char) * strlen(f->d_name) +1);
				//			testtab[i] = (char*)malloc(sizeof(char) * strlen(f->d_name));
							if (testtab[i] == NULL)
								{
									printf ("probleme B etape : %d\n", i);
								}
 							testtab[i] = f->d_name;
							i++;
							**RNDF = i;
							}
						}
					}
				} 
			else
				{
				/* probleme B */
				//printf("probleme B\n");					
				}
			}
		while (NULL != f);	
		closedir (dh);
		}
return testtab;
}

je suis sous debian lenny
merci d’avance

[ul]
[li]Pourquoi utiliser un do while ?[/li]
[li]Ton second malloc est soit inutile soit mal utilisé après. En l’état tu as un fuite mémoire. Je pense que tu devrais remplacer ça :

par ça

[li]Je te conseil de faire un cast en sortie de malloc/realloc/calloc.[/li]
[li]C’est bien realloc qui plante ?[/li][/ul]

[quote=“MisterFreez”][ul]
[li]Pourquoi utiliser un do while ?
tu utiliserais quoi toi ???
[/li]
[li]Ton second malloc est soit inutile
dans un pointeur de tableau de type char tu es bien obliger, non! :119
[/li]
[li]soit mal utilisé après.
j’ai essayer plusieurs solutions, même pb
[/li]
[li]En l’état tu as un fuite mémoire. Je pense que tu devrais remplacer ça :

testtab[i] = f->d_name;

par ça

strcpy(testtab[i], f->d_name);

ça change rien!
[/li]
[li]Je te conseil de faire un cast en sortie de malloc/realloc/calloc.
un cast ??? moi debutant
[/li]
[li]C’est bien realloc qui plante ?[/li][/ul][/quote]

un pointeur de tableau de type char est bien un tableau à 2 dimensions ???

http://c.developpez.com/sources/?page=XII

resolu

:078

    testtab = realloc(testtab, sizeof(testtab) * i + 1);