Printf qui change le résultat d'un programme ??

Résolu/Fermé
AlexAnonymous Messages postés 4 Date d'inscription jeudi 21 mars 2013 Statut Membre Dernière intervention 23 mars 2013 - 21 mars 2013 à 21:48
AlexAnonymous Messages postés 4 Date d'inscription jeudi 21 mars 2013 Statut Membre Dernière intervention 23 mars 2013 - 23 mars 2013 à 22:20
Bonsoir,

J'ai l'impression d'avoir un problème venu d'un autre monde ! Selon que je laisse ou que j'enlève un printf (en le mettant en commentaire par exemple), le résultat de mon programme change !

Je vous donne le code mais je ne pense pas que ça soit très utile.

Le résultat final devrait être 0 1 0 2 1, mais de temps en temps, j'ai des résultats bizarres avec des -1...

Encore une fois, c'est peut-être pas la peine de se jeter dans le code, surtout qu'il n'est pas commenté.

Ce en quoi vous pourriez m'aider, c'est me dire par exemple si chez vous aussi, en mettant EN METTANT EN COMMENTAIRE OU NON la ligne 137, le résultat change (la ligne afficher_matrice(*domaine, N, D); )
Vous pourriez aussi me dire dans quelles circonstances un simple printf peut changer un résultat ! Je prends l'exemple de cette ligne mais je pense que je peux arriver à des résultats bizarres en laissant / enlevant d'autres lignes ! (ce qui m'est arrivé d'ailleurs).

Merci d'avance de votre aide ! ;)

#include <stdio.h>
#include <stdlib.h>

#define N 5 // Nombre de variables : 0 -> N-1
#define M 6 // Nombre de contraintes
#define D 3 // Taille des données : 0 -> D-1

typedef struct matrice
{
int mat[100][100];
} matrice ;


/* ---------------------------------------------
*
* FONCTIONS DE GESTION DES MATRICES ET TABLEAUX
*
* --------------------------------------------- */

void init_matrice (matrice *m, int hauteur, int largeur, int valeur)
{
int i,j;
for (i=0;i<hauteur;i++)
for(j=0;j<largeur;j++)
m->mat[i][j]=valeur;
}

void init_matrice_pointeurs (matrice *m[N][N])
{
int i,j;
for (i=0;i<N;i++)
for(j=0;j<N;j++)
m[i][j]=NULL;
}

void affecter_interpretation(int interpretation[N])
{
int i;
for (i=0;i<N;i++)
interpretation[i]=-1;
}

void afficher_etat_matrice_pointeurs (matrice *matrice_pointeurs [N][N])
{
int i,j;
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
if (matrice_pointeurs[i][j]==NULL)
printf("Null ");
else
printf("Matrice ");
}
printf("\n");
}
}

void afficher_interpretation (int interpretation[N])
{
int i;
for (i=0;i<N;i++)
printf("%d ", interpretation[i]);
printf("\n");
}

void afficher_matrice (matrice m, int hauteur, int largeur)
{
int i,j;
for(i=0;i<hauteur;i++)
{
for(j=0;j<largeur;j++)
printf("%d ", m.mat[i][j]);
printf("\n");
}
}




/* -----------------------------------------------------------
*
* FONCTION D'AFFECTATIONS DES VALEURS AU PROBLÈME DES GRAPHES
*
* ----------------------------------------------------------- */

void couleur_graphe (matrice *domaine, matrice *matrice_pointeurs[N][N])
{
matrice contraintes[M];

init_matrice (domaine, N, D, 1);
init_matrice_pointeurs(matrice_pointeurs);

int i;
for (i=0;i<M;i++)
init_matrice (&contraintes[i], D, D, 1);
int j;
for (i=0;i<M;i++)
{
for(j=0;j<D;j++)
contraintes[i].mat[j][j]=0;
printf("La contrainte qui vient d'être créée est :\n");
afficher_matrice(contraintes[i], D, D);
printf("\n");
}
printf("\n\n\n");



// matrice_pointeurs[0][1]= (matrice *) malloc(sizeof(matrice *));
matrice_pointeurs[0][1]=&contraintes[0];
// matrice_pointeurs[0][4]= (matrice *) malloc(sizeof(matrice *));
matrice_pointeurs[0][4]=&contraintes[1];
// matrice_pointeurs[1][2]= (matrice *) malloc(sizeof(matrice *));
matrice_pointeurs[1][2]=&contraintes[2];
// matrice_pointeurs[1][3]= (matrice *) malloc(sizeof(matrice *));
matrice_pointeurs[1][3]=&contraintes[3];
// matrice_pointeurs[2][3]= (matrice *) malloc(sizeof(matrice *));
matrice_pointeurs[2][3]=&contraintes[4];
// matrice_pointeurs[3][4]= (matrice *) malloc(sizeof(matrice *));
matrice_pointeurs[3][4]=&contraintes[5];
}


/* ----------------------------------
*
* IMPLÉMENTATION DU FORWARD CHECKING
*
* ---------------------------------- */

void FC (matrice *domaine, matrice * matrice_pointeurs [N][N], int interpretation[N])
{
matrice tmp;
int i,j,k,l;
for(i=0;i<N;i++)
{
// afficher_matrice(*domaine, N, D); // <== CETTE DE LIGNE !
for(j=0;j<D && interpretation[i]==-1;j++)
{
if (domaine->mat[i][j]==1)
{
interpretation[i]=j;
for(k=0;k<N;k++)
{
if (matrice_pointeurs[i][k]->mat!=NULL)
{
printf("i = %d, j = %d, k = %d\n", i, j, k);
int c1, c2;
for(c1=0 ; c1<D;c1++)
for(c2=0 ;c2<D; c2++)
tmp.mat[c1][c2]=matrice_pointeurs[i][k]->mat[c1][c2];
// printf("\nContraintes :\n");
// afficher_matrice(*matrice_pointeurs[i][k], D, D);
// printf("\nFincontraintes\n");
for(l=0;l<D;l++)
{
if (tmp.mat[j][l]==0)
domaine->mat[k][l]=0;
}
}
}
}
}
if (interpretation[i]==-1)
printf("Fail !\n");
/*
if (j==D)
BT*/
}
}




int main ()
{
matrice domaine, *matrice_pointeurs[N][N];
couleur_graphe(&domaine, matrice_pointeurs);

afficher_etat_matrice_pointeurs(matrice_pointeurs);

int interpretation[N];
affecter_interpretation (interpretation);

FC (&domaine, matrice_pointeurs, interpretation);

printf("\n");
afficher_matrice(domaine, N, D);
printf("\n");
afficher_interpretation(interpretation);

return EXIT_SUCCESS;
}

3 réponses

AlexAnonymous Messages postés 4 Date d'inscription jeudi 21 mars 2013 Statut Membre Dernière intervention 23 mars 2013 1
23 mars 2013 à 22:20
Si jamais quelqu'un passe par là et rencontre un problème similaire, un autre forum m'a donné la solution :
https://forum.hardware.fr/hfr/Programmation/C/change-resultat-programme-sujet_139209_1.htm

Enjoy !
1
Salut, j'ai aussi 0 1 0 -1 1 en de-commentant, hum "dangling pointer".
0
AlexAnonymous Messages postés 4 Date d'inscription jeudi 21 mars 2013 Statut Membre Dernière intervention 23 mars 2013 1
21 mars 2013 à 23:56
Je n'arrive pas à visualiser dans quel monde un printf peut changer quelque chose. Tu as accès à ma fonction affiche_matrice, elle ne fait que des printf !
0
AlexAnonymous Messages postés 4 Date d'inscription jeudi 21 mars 2013 Statut Membre Dernière intervention 23 mars 2013 1
23 mars 2013 à 01:37
Up ! Quelqu'un d'autre pour m'aider s'il vous plaît ? :) Je suis en train de complètement réorganiser mon code et j'ai de moins en moins de bug mais bon, ça me pend au nez qu'un beau jour mon résultat soit erroné...
0