spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

C Custom Binary Search not following Multiple Of Element Size rule and crashes


You appear to be trying to pass a pointer as an integer (see how ArrayPointer is declared as an int but you are casting it to a char **. You are then manipulating the integer with the binary search. This is why you are getting whacky results.

Instead, you want either to:

  • Pass 3 arguments to StringCompare: the address of the array (which can be treated as an opaque pointer passed from BinarySearch, and two integer offsets within that array; or

  • Pass 2 arguments to StringCompare which are each void * pointers, which BinarySearch can calculate using ElementSize, the pointer to the array, and the offset.

The first is cleanest in my opinion, and doesn't require you to pass ElementSize at all, either to BinarySearch or (in turn) to StringCompare, as in each case it can work them out.

For instance:

int StringCompare (int element1, int
element2, void *opaque)
{
    char *s1 = ((char **)opaque)[element1];
    char *s2 = ((char **)opaque)[element1];
    return stricmp(s1, s2);
}

Fixing up the binary search is left as an exercise for you, but it shouldn't require any pointer arithmetic at all.

EDIT:

Whilst the passing of pointers as integers (amongst other things - see int_ptr_t if you really want to do that) makes me deeply suspicious of the code, I suspect the problem is this line:

mid = LowerBound + ElementSize *
(UpperBound - LowerBound) / ElementSize / 2;

and that the compiler is evaluating that left to right, so multiplying by ElementSize before dividing by it.

Try:

int midoffset = (UpperBound -
LowerBound) / ElementSize / 2;
mid = LowerBound + ElementSize * midoffset;

Categories : C

Related to : C Custom Binary Search not following Multiple Of Element Size rule and crashes
How Can I Search A Word in A Binary Search Tree?
You'll have to walk the whole tree since you're not searching by id, but you seem to realize that already. To walk the tree in order using recursion, recursively call your function on the left subtree, process the current node, then recursively call the function on the right subtree (checking for NULL appropriately). Example: void listWord(node *tree, char *word) { if (tree) { /* I

Categories : C
Binary search on list
List has a binary search method but since binary search is so easy to implement and since the IComparator you would need to define is so complex due to the range thing I suggest you implement the binary search method Something like this (NOT TESTED!) public static IPRange BinarySearch(List<IPRange> source, int intIp) { int startIndex = 0; int endIndex = source.Count; while (end

Categories : C#
Recursive Binary Search in C++
There are two problems with your code: In your binarySearch() method, you are comparing values, not keys. This may be what you wanted but you don't have value 2 stored in your list (see point 2). You insertAfter() method inserts chars that when printed give 0,1,2,3,4,5,6 but in fact their int values (based on which you make the comparison) are the following: 48,49,50,51,52,53,54. I am not sure

Categories : C++
how to add a node in a binary search tree
So 2 things NoneType is capitalized: None (all if's you do in create_node) Python string comparison is casesensitive, so when you check for pointer["number"] value you are not really checking the one you set in the create_node method, which it's key is "Number"(capital "N"), being the reason it throws KeyError. Same for "right" and "left".

Categories : Python
C++: Binary search tree end() iterator
The end iterator doesn't necessarily have to be one past the last element (that makes sense for vectors, but less so for trees eg.). It has to just be an iterator that can clearly be identified as not a valid iterator used for indicating that the end of the data structure is reached. Practically, this can be done in several ways, depending on how your iterator refers to what it's pointing to. If

Categories : C++
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.