[debugage] besoin daide langage C

Fermé
paulina82 Messages postés 1 Date d'inscription vendredi 3 juin 2005 Statut Membre Dernière intervention 3 avril 2006 - 3 avril 2006 à 12:09
 Baztien - 5 avril 2006 à 19:38
Bonjour,
j'ai un gros probleme avec mon programme ecri en langage C.lorsque je complile il n'afiche pa d'erreur mais lorsque j'esaye de l'executé il veut pas.Si quelqu'un pourais m'aidé je seré trés reconesante car c pour un projet. je vous remerci d'avance.
le programme est le suivant :

#include <stdio.h>
#define N 5
#define M 5




void affiche (char tab_test[N][M],int lig,int col){
int i , j;

for(i=0;i<lig;i++){
for(j=0;j<col;j++){
printf("%c ",tab_test[i][j]);
}
}
printf("\n");
}




void affiche2 (int tab_test[N][M],int lig,int col){
int i , j;

for(i=0;i<lig;i++){
for(j=0;j<col;j++){
printf("%c ",tab_test[i][j]);
}
}
printf("\n");
}



void init_tab ( char tab[N][M],int lig,int col,char car){


int i,j;
int k,l;


for(i=0;i<lig;i++){
for(j=0;j<col;j++){
tab[i][j]=car;

}
}
//affiche(tab,i,j);
}


void init_tab2 ( int tab[N][M],int lig,int col,int car){


int i,j;
int k,l;


for(i=0;i<lig;i++){
for(j=0;j<col;j++){
tab[i][j]=car;

}
}
//affiche(tab,i,j);
}


void nb_occurences(char image[N][M],int lig,int col){

char tablo[N][M];
int tb_occ[N][M];

int i,j;
int ligne=0;
int k;


init_tab(tablo,N,3,'\0');
init_tab2(tb_occ,N,1,0);

for(i=0;i<lig;i++){
for(j=0;j<col;j++){

for(k=0;k<3;k++){

if(image[i][j]==tablo[k][1] ){






tb_occ[k][1] ++;
}
else {
tb_occ[ligne][1] ++;
tablo[ligne][1]=image[i][j];
ligne ++;
}
}
}
}
affiche(tablo,5,3);
affiche2(tb_occ,5,1);
}



int main(){

char tab[N][M];
char tab_test[N][M]={{'a','b'},{'c','d'}};
char image[N][M]={{'a','b','c','d','e'} , {'a','a','c','d','e'},{'b','b','c','a','e'},{'a','b','c','d','e'},{'a','b','c','d','e'}};

//6 a 5 b 5 c 4 d 5 e

affiche(tab_test,2,2);
init_tab(tab,3,3,'b');
init_tab(tab,N,4,'\0');

nb_occurences(image ,5,5);


return 0;
}
A voir également:

2 réponses

mamiemando Messages postés 33093 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 4 mai 2024 7 752
5 avril 2006 à 09:34
On compile :
(mando@aldur) (~) $ gcc -g -W -Wall plop.c
plop.c: In function ‘init_tab’:
plop.c:39: warning: unused variable ‘l’
plop.c:39: warning: unused variable ‘k’
plop.c: In function ‘init_tab2’:
plop.c:56: warning: unused variable ‘l’
plop.c:56: warning: unused variable ‘k’

Tu peux déjà corriger ces warnings ;-) Ensuite on lance le bazar avec un debuggeur pour voir où ca plante :
(mando@aldur) (~) $ gdb a.out
GNU gdb 6.4-debian
Copyright 2005 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) r
Starting program: /home/mando/a.out
a b c d

Program received signal SIGSEGV, Segmentation fault.
0x08048586 in nb_occurences (image=0xbff4cc35, lig=5, col=5) at plop.c:87
87                                      if(image[i][j]==tablo[k][1] ){
(gdb) bt
#0  0x08048586 in nb_occurences (image=0xbff4cc35, lig=5, col=5) at plop.c:87
#1  0x0804874c in main () at plop.c:122
(gdb)

Donc ca plante ligne 122 (appel de nb_occurences), plus précisemmment ligne 87
    for(i=0;i<lig;i++){
        for(j=0;j<col;j++){

            for(k=0;k<3;k++){

                if(image[i][j]==tablo[k][1] ){

Je n'ai pas le temps de regarder plus en détail car je dois aller travailler, mais a priori il faudrait contrôler les valeurs de i,j,k,l et vérifier qu'elles ne sont pas en dehors du tableau.

Bonne chance
0
Hello,

Je n'ai pas trop compris ce que tu cherchais précisement à faire... anyway

En fait, tu ne remet jamais à zéro ta variable ligne, donc tes lignes :
tb_occ[ligne][1] ++;
tablo[ligne][1]=image[i][j];
écrivent à des endroits de la mémoire qui ne sont pas réservés, c'est à cause de ça que se produit l'erreur de segmentation.

C'est effectivment piégeant parce que le SIGSEGV semble se produire sur image[i][j]==tablo[k][1] alors que le problème n'est pas là.

Bon courage :-)

A+,
Baztien
0