B
beagle197
Folks,
Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...}
allocated using malloc/calloc, but the arguments I'm passing to qsort
are producing the incorrect results (see listing 1). Works for auto
object duration (e.g. heap) as in listing 2. Any ideas?
Thanks,
BEA
/* listing 1 : using malloc/calloc */
#include <stdio.h>
#include <stdlib.h>
int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);
if (((int *) arg1)[0] > ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] > ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}
int main(int argc, char *argv[]) {
int i;
int **x;
x = malloc(6 * sizeof(int *));
for (i = 0; i < 6; ++i) {
x = calloc(2, sizeof(int));
}
/* assignments */
x[0][0] = 1;
x[0][1] = 3;
x[1][0] = 0;
x[1][1] = 2;
x[2][0] = 2;
x[2][1] = 6;
x[3][0] = 0;
x[3][1] = 1;
x[4][0] = 1;
x[4][1] = 7;
x[5][0] = 2;
x[5][1] = 1;
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[0], x[1]);
printf("\n");
fflush(stdout);
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );
printf("\n");
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[0], x[1]);
for (i = 0; i < 6; ++i) {
free(x);
}
free(x);
}
=====
/* listing 2: using heap store */
#include <stdlib.h>
int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
if (((int *) arg1)[0] > ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] > ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}
int main(int argc, char *argv[]) {
int i;
int x[6][2] = {
{1,3},
{0,2},
{2,6},
{0,1},
{1,7},
{2,1}};
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[0], x[1]);
printf("\n");
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );
printf("\n");
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[0], x[1]);
}
Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...}
allocated using malloc/calloc, but the arguments I'm passing to qsort
are producing the incorrect results (see listing 1). Works for auto
object duration (e.g. heap) as in listing 2. Any ideas?
Thanks,
BEA
/* listing 1 : using malloc/calloc */
#include <stdio.h>
#include <stdlib.h>
int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
fflush(stdout);
if (((int *) arg1)[0] > ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] > ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}
int main(int argc, char *argv[]) {
int i;
int **x;
x = malloc(6 * sizeof(int *));
for (i = 0; i < 6; ++i) {
x = calloc(2, sizeof(int));
}
/* assignments */
x[0][0] = 1;
x[0][1] = 3;
x[1][0] = 0;
x[1][1] = 2;
x[2][0] = 2;
x[2][1] = 6;
x[3][0] = 0;
x[3][1] = 1;
x[4][0] = 1;
x[4][1] = 7;
x[5][0] = 2;
x[5][1] = 1;
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[0], x[1]);
printf("\n");
fflush(stdout);
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );
printf("\n");
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[0], x[1]);
for (i = 0; i < 6; ++i) {
free(x);
}
free(x);
}
=====
/* listing 2: using heap store */
#include <stdlib.h>
int compare( const void *arg1, const void *arg2 ) {
printf("arg1 %d, %d\n", ((int *) arg1)[0], ((int *) arg1)[1]);
printf("arg2 %d, %d\n", ((int *) arg2)[0], ((int *) arg2)[1]);
if (((int *) arg1)[0] > ((int *) arg2)[0]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
if (((int *) arg1)[1] > ((int *) arg2)[1]) {
return -1;
} else if (((int *) arg1)[0] == ((int *) arg2)[0]) {
return 0;
} else if (((int *) arg1)[0] < ((int *) arg2)[0]) {
return 1;
}
} else
return 1;
}
int main(int argc, char *argv[]) {
int i;
int x[6][2] = {
{1,3},
{0,2},
{2,6},
{0,1},
{1,7},
{2,1}};
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[0], x[1]);
printf("\n");
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)x, 6, sizeof( x[0] ), compare );
printf("\n");
for (i = 0; i < 6; ++i)
printf("%d, %d\n", x[0], x[1]);
}