[C] erreur de segmentation

Fermé
tophe03 Messages postés 327 Date d'inscription lundi 24 janvier 2005 Statut Membre Dernière intervention 14 avril 2010 - 21 nov. 2006 à 20:40
tophe03 Messages postés 327 Date d'inscription lundi 24 janvier 2005 Statut Membre Dernière intervention 14 avril 2010 - 28 nov. 2006 à 13:27
Bonjour,

J'ai un programme qui se compile très bien.
Lorsque je l'exécute, le prog plante. Lorsque j'uitilise le debugger, au bout d'un certain temps d'eécution, j'ai un message du style "vilation d'accès (erreur de segmentation)...". Qu'est-ce qu cela signifie?

Merci

7 réponses

mamiemando Messages postés 33076 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 17 avril 2024 7 748
22 nov. 2006 à 01:02
Celà signifie que tu accède à une zone mémoire non allouée (erreur typique en C quand on commence à manipuler des pointeurs ou des tableaux).

Exemple :
#include <stdio.h>

int main(){
  int tab[5];
  printf("%d\n",tab[69]); //seg fault car tab[69] est hors du tableau
  int *x;
  *x = 28; // seg fault, car *x est un entier non alloué
  return 0;
}

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

int main(){
  int tab[70];
  printf("%d\n",tab[69]);
  int *x =(int*)malloc(sizeof(int));
  *x = 28;
  return 0;
}

Comment détecter une segmentation fault avec gcc+gdb :
gcc -g -W -Wall -o plop.exe plop.c
gdb plop.exe

Dans gdb :
r
bt

A noter que pour windows dev cpp est basé sur gcc...

Bonne chance
3
tophe03 Messages postés 327 Date d'inscription lundi 24 janvier 2005 Statut Membre Dernière intervention 14 avril 2010 5
22 nov. 2006 à 20:47
Bonjour

Je vois pas trop où j'aurais pu faire une erreur.
Voici une partie du code:

double *saisie_pol(HINSTANCE hinst, HWND hEdit, double *coefs, int deg)
{
    
	for(q=deg;q>=0;q--)
	{
         
      DialogBoxParam(hinst, "DIALOG4", hEdit, (DLGPROC)Dialog4Proc, (LPARAM)hEdit);
      
      coefs[q] = *cfs;
      free(cfs);

	}
	
	return coefs;
}

char *saisie_degre(HINSTANCE hinst, HWND hEdit, polynome *p, double *coefs, int deg, int nb_poly, char *szBuffer1)
{
     int m = 0;
     char *szBuffer;
     szBuffer = (char*)malloc(256*sizeof(char));
     
     if (nb_poly == 0)
          deg = (int)DialogBoxParam(hinst, "DIALOG2", hEdit, (DLGPROC)Dialog2Proc, (LPARAM)hEdit);
     else if (nb_poly == 1)
          deg = (int)DialogBoxParam(hinst, "DIALOG3", hEdit, (DLGPROC)Dialog3Proc, (LPARAM)hEdit);
     new_pol(&p[nb_poly],deg);
     saisie_pol(hinst, hEdit, coefs, deg);
     init_pol(&p[nb_poly],coefs);
     m += sprintf(szBuffer, "p%d(x) = %s", nb_poly + 1, afficher(&p[nb_poly], szBuffer1));
     free(szBuffer1);
     
     return szBuffer;
}

void MainWnd_Command(HINSTANCE hinst, HWND hEdit, WPARAM wParam)
{
     char *buf, *temp;
     int m, deg, nb_poly;
     double *coefs, eval, x;
     polynome *tampon, *p;
     
     buf = (char*)malloc(256*sizeof(char));
     temp = (char*)malloc(256*sizeof(char));
     coefs = (double*)malloc((deg+1)*sizeof(double));
     tampon = (polynome*)malloc(2*sizeof(polynome));
     p = (polynome*)malloc(2*sizeof(polynome));

     m = 0;

    if (LOWORD(wParam) == IDM_DERIV)
     {

           m = sprintf(buf, "Polynôme à dériver:\r\n    %s", saisie_degre(hinst, hEdit, &p[0], coefs, deg, 0, temp));
           free(temp);
           tampon[0] = derive(p[0]);
           m += sprintf(buf + m, "\r\n\r\n    p1'(x) = %s", afficher(&tampon[0], temp));
           free(temp);
     }

else if (LOWORD(wParam) == IDM_SOMME)
     {

           m = sprintf(buf + m, "Polynômes à ajouter:\r\n    %s\r\n    ", saisie_degre(hinst, hEdit, &p[0], coefs, deg, 0, temp));
           free(temp);
           m += sprintf(buf + m, "%s", saisie_degre(hinst, hEdit, &p[1], coefs, deg, 1, temp));
           free(temp);
           tampon[0] = add(p[0],p[1]);
           m += sprintf(buf + m, "\r\n\r\n    p1(x) + p2(x) = %s", afficher(&tampon[0], temp));
           free(temp);
     }
  SetWindowText(hEdit, buf);
  free(tampon);
  free(p);
  free(buf);
}


Pour ce qui est de la dérivation (IDM_DERIV), ça marche.
Pour la somme (IDM_SOMME), c'est là que j'ai l'erreur.
Si j'intercale des "SetWindowText(hEdit, buf);" dans IDM_SOMME, les polynomes p[0] et p[1] s'affichent correctement. C'est après que ça se gâte, au niveau de tampon[0]=add(p[0],p[1]).

Si tu vois qcc de louche...
Merci
0