1

Hello I have some problems with the bsearch() function. I get the "Access violation reading location" exception. I'm using it as follows:

typedef char **arstr;

int compareexp(const void *a, const void *b){ 
    return strcmp(*(const arstr)a, *(const arstr)b);
}

binsrch = bsearch(&key, file_array, linecount, WORDMAX+2, compareexp);

'file_array' is my dynamic string array with 2401 elements. each element is max 22 chars long imported from a *.txt document.

'linecount' is the integer 2402 and refers to the line number from the same *.txt document.

'WORDMAX' is defined as 20

I don't really get the reason why my function is failing. I guess there is something wrong with the values from linecount or WORDMAX.

alk
  • 69,737
  • 10
  • 105
  • 255
Frozen_byte
  • 272
  • 4
  • 12
  • it is the comparator Argument of my bsearch() as explained here http://www.cplusplus.com/reference/clibrary/cstdlib/bsearch/ – Frozen_byte Apr 18 '12 at 01:06

1 Answers1

0

After looking at the example from the like it seems like you either might be missing a * in the comperison or WORDMAX +2 not is the right approach. Try changing that WORDMAX to get the size of the struct you are using.

chikuba
  • 4,229
  • 6
  • 43
  • 75
  • file_array is filled as follows: " strcpy(file_array[i], ln); " ln is defined as " char ln[WORDMAX+2]; " so every Item should have 22 bits. Where is the * missing? I have copied the comparesion therm from there: http://bytes.com/topic/c/answers/667528-bsearch-dynamic-array – Frozen_byte Apr 18 '12 at 01:16
  • so every char array is 22 chars long? if thats correct, the char arrays size will not be 22 bytes – chikuba Apr 18 '12 at 01:20
  • You where right, the correct value for the size is "4". Got that due Trial and Error metod and have no serious explanations for it... – Frozen_byte Apr 18 '12 at 01:26
  • so its working now? i see where you got 22 from (just looked more at the link you gave me). but also, the size might be the size of key, not the size of the object in the array. how is your key definied? if you are dealing with strings, it might be something wrong with the key – chikuba Apr 18 '12 at 01:54
  • My key is fetched from user input it has a variable length up to 20. At the moment my test are running fine with "sizeof *file_array" as size parameter. – Frozen_byte Apr 18 '12 at 02:00
  • i was more thinking about you sending "&key" instead of "key". have a look at the example on the link where they work with strings instead of ints :) – chikuba Apr 18 '12 at 02:02
  • my Key &permutations_array on Watch is: "0x013c9154 {WordPermutator.exe!char * *permutations_array} {0x003f9e00 {0x003f9ea0 "abby"}} char * * *" while the key without & is as follows: "permutations_array 0x003f9e00 {0x003f9ea0 "abby"} char * *" Don't think there is a major difference – Frozen_byte Apr 18 '12 at 02:25
  • i do belive that it is. the search function takes key as a pointer which forces you to send varibles though their memory adress and arrays by their pointer. unlikley that it wont do any difference if you send an adress to a pointer or the pointer. – chikuba Apr 18 '12 at 02:28
  • unlikely the Access Violation Error appears again, if I remove the '&' so it may not be deleted *g* – Frozen_byte Apr 18 '12 at 02:32