Dear fellow coders,
I've been having problems with this piece of code and I can't seem to figure out the cause. I'd really appreciate it if someone could point out why I'm getting errors here..
Code:
typedef struct {
char *name;
unsigned long match_num;
unsigned long *match_support;
struct graphNode **link;
} graphNode;
int node_compare(const void *a, const void *b) {
return strcmp(((graphNode*)a)->name, ((graphNode*)b)->name);
}
graphNode **NODE_INDEX = (graphNode**) malloc(SHOTGUN_READ_NUM*sizeof(graphNode*));
//SNIP
qsort(NODE_INDEX, SHOTGUN_READ_NUM, sizeof(graphNode*), node_compare); // sort shotgun fragments by name
// SNIP
for (count=0; count<match_count_5; count++) {
graphNode *a = malloc(sizeof(graphNode));
a->name = name_array_5[count]; // points a->name to a string in memory
graphNode *fromNode = bsearch(a, NODE_INDEX, SHOTGUN_READ_NUM, sizeof(graphNode*), node_compare);
free(a);
//SNIP
gdb shows that in node_compare(), the values pointed to by a->name and b->name are garbage, or sometimes out of bounds.
Anyone?