On 27 juil, 15:23, -ed- <emmanuel.delah...@gmail.com> wrote:
> Ce qui compte, ce n'est pas l'historique, mais l'=E9tat des lieux tel
> qu'il est affirm=E9 =E0 ce jour, c'est =E0 dire, par exemple, sur mon
> site ...
Tiens justement, j'avais =E0 coder, en r=E9ponse =E0 une question du forum =
C
du Site du Z=E9ro, une fonction callback compar() pour un qsort()
d'entiers. J'ai donc eu l'id=E9e de regarder ce que tu proposais sur ton
site. Voici ton code, cf. http://www.bien-programmer.fr/qsort.htm :
/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D*/
/* fonction utilisateur de comparaison fournie a qsort() */
static int compare (void const *a, void const *b)
{
/* definir des pointeurs type's et initialise's
avec les parametres */
int const *pa =3D a;
int const *pb =3D b;
/* evaluer et retourner l'etat de l'evaluation (tri croissant) */
return *pa - *pb;
}
/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D*/
Il me semble que ce code contient une erreur : *pa - *pb peut
engendrer un d=E9passement de capacit=E9 (integer overflow), ce qui est un
comportement ind=E9termin=E9.
/* fonction utilisateur de comparaison fournie a qsort() */
static int compare (void const *a, void const *b)
{
/* definir des pointeurs type's et initialise's
avec les parametres */
int const *pa =3D a;
int const *pb =3D b;
/* evaluer et retourner l'etat de l'evaluation (tri croissant) */
return *pa - *pb;
}
static int int_compare(const void* p, const void* q) { const int a = *(const int*)p; const int b = *(const int*)q; if (a < b) return -1; if (a > b) return 1; return 0; }
static int
int_compare(const void* p, const void* q) {
const int a = *(const int*)p;
const int b = *(const int*)q;
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
static int int_compare(const void* p, const void* q) { const int a = *(const int*)p; const int b = *(const int*)q; if (a < b) return -1; if (a > b) return 1; return 0; }
-- Gaby
-ed-
On 28 juil, 19:16, Gabriel Dos Reis wrote:
J'aurais écrit
static int int_compare(const void* p, const void* q) { const int a = *(const int*)p; const int b = *(const int*)q; if (a < b) return -1; if (a > b) return 1; return 0; }
-- Gaby
OK.
On 28 juil, 19:16, Gabriel Dos Reis <g...@cs.tamu.edu> wrote:
J'aurais écrit
static int
int_compare(const void* p, const void* q) {
const int a = *(const int*)p;
const int b = *(const int*)q;
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
static int int_compare(const void* p, const void* q) { const int a = *(const int*)p; const int b = *(const int*)q; if (a < b) return -1; if (a > b) return 1; return 0; }
-- Gaby
OK.
Antoine Leca
Le 28/07/2009 17:16Z, Gabriel Dos Reis écrivit :
J'aurais écrit
static int int_compare(const void* p, const void* q) { const int a = *(const int*)p; const int b = *(const int*)q; if (a < b) return -1; if (a > b) return 1; return 0;
C'est sûr que c'est plus lisible que
return a<b ? -1 : a>b;
mais est-ce aussi efficace (avec les compilos actuels) ?
Antoine
Le 28/07/2009 17:16Z, Gabriel Dos Reis écrivit :
J'aurais écrit
static int
int_compare(const void* p, const void* q) {
const int a = *(const int*)p;
const int b = *(const int*)q;
if (a < b) return -1;
if (a > b) return 1;
return 0;
C'est sûr que c'est plus lisible que
return a<b ? -1 : a>b;
mais est-ce aussi efficace (avec les compilos actuels) ?
static int int_compare(const void* p, const void* q) { const int a = *(const int*)p; const int b = *(const int*)q; if (a < b) return -1; if (a > b) return 1; return 0;
C'est sûr que c'est plus lisible que
return a<b ? -1 : a>b;
mais est-ce aussi efficace (avec les compilos actuels) ?