Bonjour,
voici un programme en C. L'erreur se produit tout à la fin avec un free(...). Je ne comprends pas (je débute). Si vous pouviez m'aider, merci d'avance.
#include <stdio.h>
#include <stdlib.h>
int sommeMax(int nbre,int res);
int sommeMin(int nbre,int res);
int sommePossible(int nbre, int somme);
void afficheTableau(int tab[],int tailleTab);
void copieTableau(int tabDep[],int tabArr[],int tailleTab);
void ajouterUnElement(int tabDep[],int tabArr[],int nbreAAjouter,int tailleTabDep);
int *appendPerso(int tab[],int nbreAAjouter,int tailleTab);
void afficherTabDeTab(int **tab,int tailleTab,int tailleTabDsTab);
void copieTabDeTab(int **tabDep,int **tabArr,int tailleTab,int tailleTabDsTab);
int **ajoutUnTabDsTabDeTab(int **tab,int *tabAAjouter,int tailleTab,int tailleTabDsTab)
{
int **tabTemp=NULL;
int j;
tabTemp=malloc((tailleTab+1)*sizeof(*tabTemp));
if (tabTemp==NULL)
{
printf("Erreur 5");
exit(0);
}
for(j=0;j<tailleTabDsTab;j++)
{
tabTemp[j]=malloc(tailleTabDsTab*sizeof(**tabTemp));
if (tabTemp[j]==NULL)
{
for(j=j-1;j>=0;j--)
{
free(tabTemp[j]);
}
free(tabTemp);
printf("Erreur 6");
exit(0);
}
}
copieTabDeTab(tab,tabTemp,tailleTab,tailleTabDsTab);
for(j=0;j<tailleTabDsTab;j++)
{
tabTemp[tailleTab][j]=tabAAjouter[j];
}
for(j=0;j<tailleTab;j++)
{
free(tab[j]);
}
free(tab);
tab=NULL;
return tabTemp;
}
int main()
{
int nbreDeTerme=1;
int resultat;
int sommeChoisie;
int j;
int nbreDeCol=2;
int *listReponse= NULL;
int **tabEss;
int **tabEss2;
int listReponse2[]={1,2,3,7,8};
int listReponse3[]={2,4,8,16,32};
printf("En combien de nombre voulez vous decomposer ?\n");
do
{
scanf("%d",&nbreDeTerme);
} while ((nbreDeTerme<1)||(nbreDeTerme>9));
resultat=sommeMax(nbreDeTerme,9);
printf("Vous aurez au maximum comme somme %d .\n",resultat);
resultat=sommeMin(nbreDeTerme,1);
printf("Vous aurez au minimum comme somme %d .\n",resultat);
printf("Quel nombre voulez-vous atteindre ? \n");
scanf("%d",&sommeChoisie);
if (sommePossible(nbreDeTerme,sommeChoisie))
{
printf("Il est possible d'ecrire %d en %d termes distincts. \n",sommeChoisie,nbreDeTerme);
}
else
{
printf("Il n'est pas possible d'ecrire %d en %d termes distincts. \n",sommeChoisie,nbreDeTerme);
}
listReponse=malloc(nbreDeTerme*sizeof(*listReponse));
if (listReponse==NULL)
{
exit(0);
}
listReponse[0]=8;
listReponse[1]=32;
tabEss=malloc(nbreDeCol*sizeof(*tabEss));
if (tabEss==NULL)
{
printf("Erreur 1");
exit(0);
}
for(j=0;j<nbreDeCol;j++)
{
tabEss[j]=malloc(nbreDeTerme*sizeof(**tabEss));
if (tabEss[j]==NULL)
{
for(j=j-1;j>=0;j--)
{
free(tabEss[j]);
}
free(tabEss);
printf("Erreur 2");
exit(0);
}
}
tabEss2=malloc(nbreDeCol*sizeof(*tabEss2));
if (tabEss2==NULL)
{
printf("Erreur 3");
exit(0);
}
for(j=0;j<nbreDeCol;j++)
{
tabEss2[j]=malloc(nbreDeTerme*sizeof(**tabEss2));
if (tabEss2[j]==NULL)
{
for(j=j-1;j>=0;j--)
{
free(tabEss2[j]);
}
free(tabEss2);
printf("Erreur 4");
exit(0);
}
}
for(j=0;j<nbreDeTerme;j++)
{
tabEss[0][j]=listReponse[j];
}
for(j=0;j<nbreDeTerme;j++)
{
tabEss[1][j]=listReponse2[j];
}
copieTabDeTab(tabEss,tabEss2,nbreDeCol,nbreDeTerme);
printf("Tableau 1 \n");
afficherTabDeTab(tabEss,nbreDeCol,nbreDeTerme);
printf("La copie \n");
afficherTabDeTab(tabEss2,nbreDeCol,nbreDeTerme);
printf("Avec un de plus \n");
tabEss=ajoutUnTabDsTabDeTab(tabEss,listReponse3,nbreDeCol,nbreDeTerme);
nbreDeCol=nbreDeCol+1;
afficherTabDeTab(tabEss,nbreDeCol,nbreDeTerme); // Jusqu'ici pas de problème
for(j=0;j<nbreDeCol;j++) // ici erreur segmentation fault pour j=0
{
free(tabEss[j]);
}
free(tabEss);
tabEss=NULL;
for(j=0;j<nbreDeCol;j++)
{
free(tabEss2[j]);
}
free(tabEss2);
tabEss2=NULL;
free(listReponse);
return 0;
}