I am writing a program that sorts an array of either floats or doubles -- this is specified at compile time via a command to the compiler. I also must use void pointers. My program compiles, but when I try to run it with floats I get a "segmentation fault" and when i try to run it with doubles I get a "illegal instruction" error message. Any suggestions?? Thanks in advance for your help!!
Here is the main, heapSort file, and header file: (the program also contains files for insert, merge, and bubble which you will see in the main and header but I am only concerned with the heap sort file)
//header file
#ifndef SRT_H
#define SRT_H
#include <string.h>
#define MAX_BUF 256
#define swap(qx,qy,sz) \
do { \
char buf[MAX_BUF]; \
char *q1 = qx; \
char *q2 = qy; \
for (size_t m, ms = sz; ms > 0; ms -= m, q1 += m, q2 += m) { \
m = ms < sizeof(buf) ? ms : sizeof(buf); \
memcpy(buf, q1, m); \
memcpy(q1, q2, m); \
memcpy(q2, buf, m); \
} \
} while (0)
void srtbubb(void *, size_t, size_t, int (*)(const void *, const void *));
void srtheap(void *, size_t, size_t, int (*)(const void *, const void *));
void srtinsr(void *, size_t, size_t, int (*)(const void *, const void *));
void srtmerg(void *, size_t, size_t, int (*)(const void *, const void *));
#endif /* SRT_H */
//main
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include "srt.h"
int compare(const void *, const void *);
int main(int argc, char *argv[]) {
int size = argc == 2 ? atoi(argv[1]) : SHRT_MAX;
TYPE *a = calloc(size, sizeof(TYPE));
#ifdef RAND
for (int i = 0; i < size; ++i) {
a[i] = (TYPE)rand() / RAND_MAX;
}
#else
for (int i = 0; i < size; ++i) {
a[i] = i;
}
#endif
#if defined BUBB
srtbubb(a, size, sizeof(TYPE), compare);
#elif defined HEAP
srtheap(a, size, sizeof(TYPE), compare);
#elif defined INSR
srtinsr(a, size, sizeof(TYPE), compare);
#elif defined MERG
srtmerg(a, size, sizeof(TYPE), compare);
#else
qsort(a, size, sizeof(TYPE), compare);
#endif
#ifdef PRNT
for (int i = 0; i < size; ++i) {
printf("%f\n", a[i]);
}
#else
for (int i = 0; i < size - 1; ++i) {
if (a[i] > a[i + 1]) {
printf("fail\n");
goto end;
}
}
printf("pass\n");
#endif
end:
free(a);
return 0;
}
int compare(const void *p1, const void *p2) {
if (*(TYPE *)p1 < *(TYPE *)p2) {
return -5;
}
else if (*(TYPE *)p1 > *(TYPE *)p2) {
return +5;
}
return 0;
}
//heap sort file
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include "srt.h"
void heapify (void *, size_t, size_t);
void siftdown(void *, char *, char *, size_t, size_t);
void srtheap(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *)) {
heapify(base, nelem, size);
char *pEnd = (char *)base + ((nelem - 1) * size);
while (pEnd > 0){
swap(pEnd, base, size);
pEnd -= size;
siftdown(base, 0, pEnd, size, nelem);
}
}
void heapify(void *pBase, size_t nelem, size_t size){
char *pStart = (char *)pBase + (((nelem-2)/2) * size);
char *pEnd = (char *)pBase + ((nelem - 1) * size);
while (pStart >= 0){
pEnd -= size;
siftdown(pBase, pStart, pEnd, size, nelem);
pStart -= size;
}
return;
}
void siftdown(void *pBase, char * pStart, char * pEnd, size_t size, size_t nelem){
char *pRoot;
pRoot = pStart;
size_t l = (nelem / 2);
size_t r = (nelem - 1);
while ((pRoot + (size * l)) <= pEnd){
char *pChild;
pChild = (pRoot + (size * l));
if (((pChild + size) <= pEnd) && (pChild < (pChild + size)))
pChild += size;
if (pRoot < pChild){
swap(pRoot, pChild, size);
pRoot = pChild;
}
else
return;
}
return;
}