lundi 2 mars 2015

'names' string disappears after qsort with 'Symbol' struct



So I get the ELF function symbols from a file, put them into a new Symbol struct, and sort an array of Symbols alphabetically by name. I use strdup to allocate memory for my string


before qsort:



00000000 00000015 t crash_here
00000015 00000014 t winky
00000036 0000002e t dinky
00000029 0000000d T pinky
00000064 00000014 T binky
00000078 00000017 T main


after qsort:



00000064 00000014 T
00000000 00000015 t
00000036 0000002e t
00000078 00000017 T
00000029 0000000d T
00000015 00000014 t


What could have gone wrong?! Here is more code for context:



FILE *fp;
fp = fopen(argv[1], "r");
if(!fp) error(1, 0, "'%s': no such file", argv[1]); //check for valid file
size_t nelems = 0;
Symbol *symbs = (Symbol *)getFunctionSymbols(fp, &nelems);
printAllSymbols(symbs, nelems);
qsort(symbs , nelems, sizeof(Symbol)* sizeof(Symbol), compareSymbAlph);
printAllSymbols(symbs, nelems);

int compareSymbAlph(const void *a, const void *b){
Symbol *first = (Symbol *)a;
Symbol *second = (Symbol *)b;
return strcmp(first->name, second->name);
}



Aucun commentaire:

Enregistrer un commentaire