I have a program (complete source at the end) which correctly outputs this:
["charlie" "foxtrot" "oscar" "echo" "alpha" "golf" "tango" "delta" "bravo"] ["alpha" "bravo" "charlie" "delta" "echo" "foxtrot" "golf" "oscar" "tango"] check_array OK
check_index_found true OK
check_index_found false OK
However, if you uncomment the "//#define BUG" line, the output (in gdb) is this:
(gdb) run
Starting program: /home/mark/tmp/mycmpstr/mycmpstr
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". ["charlie" "foxtrot" "oscar" "echo" "alpha" "golf" "tango" "delta" "bravo"] ["alpha" "bravo" "charlie" "delta" "echo" "foxtrot" "golf" "oscar" "tango"] check_array OK
Program received signal SIGSEGV, Segmentation fault.
__strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:283
283 ../sysdeps/x86_64/multiarch/strcmp-avx2.S: No such file or directory. (gdb) bt
#0 __strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:283
#1 0x00005555555553e0 in mystrcmp (s=0x555555556030, t=0x7fffffffde10) at mycmpstr.c:50
#2 0x00007ffff7e0a53c in __GI_bsearch (__key=0x555555556030, __base=0x7fffffffddf0,
__nmemb=<optimized out>, __size=8, __compar=0x5555555553b7 <mystrcmp>)
at ../bits/stdlib-bsearch.h:33
#3 0x0000555555555317 in main () at mycmpstr.c:30
The difference is that without BUG defined I use my own binary search,
but with BUG defined I use bsearch.
[...]
// mystrcmp segfaults:
char* p = bsearch("oscar", words, size, sizeof(char*), mystrcmp);
index = p - words[0];
found = p != NULL:
I have a program (complete source at the end) which correctly outputs this:
["charlie" "foxtrot" "oscar" "echo" "alpha" "golf" "tango" "delta" "bravo"] ["alpha" "bravo" "charlie" "delta" "echo" "foxtrot" "golf" "oscar" "tango"] check_array OK
check_index_found true OK
check_index_found false OK
However, if you uncomment the "//#define BUG" line, the output (in gdb) is this:
(gdb) run
Starting program: /home/mark/tmp/mycmpstr/mycmpstr
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". ["charlie" "foxtrot" "oscar" "echo" "alpha" "golf" "tango" "delta" "bravo"] ["alpha" "bravo" "charlie" "delta" "echo" "foxtrot" "golf" "oscar" "tango"] check_array OK
Program received signal SIGSEGV, Segmentation fault.
__strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:283
283 ../sysdeps/x86_64/multiarch/strcmp-avx2.S: No such file or directory. (gdb) bt
#0 __strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:283
#1 0x00005555555553e0 in mystrcmp (s=0x555555556030, t=0x7fffffffde10) at mycmpstr.c:50
#2 0x00007ffff7e0a53c in __GI_bsearch (__key=0x555555556030, __base=0x7fffffffddf0,
__nmemb=<optimized out>, __size=8, __compar=0x5555555553b7 <mystrcmp>)
at ../bits/stdlib-bsearch.h:33
#3 0x0000555555555317 in main () at mycmpstr.c:30
The difference is that without BUG defined I use my own binary search,
but with BUG defined I use bsearch.
[...]
int mystrcmp(const void* s, const void* t) {
return strcmp(*(const char**)s, *(const char**)t);
}
On 15/08/2024 06:56, Mark Summerfield wrote:
I have a program (complete source at the end) which correctly
outputs this:
["charlie" "foxtrot" "oscar" "echo" "alpha" "golf" "tango" "delta"
"bravo"] ["alpha" "bravo" "charlie" "delta" "echo" "foxtrot" "golf"
"oscar" "tango"] check_array OK
check_index_found true OK
check_index_found false OK
However, if you uncomment the "//#define BUG" line, the output (in
gdb) is this:
(gdb) run
Starting program: /home/mark/tmp/mycmpstr/mycmpstr
[Thread debugging using libthread_db enabled]
Using host libthread_db library
"/lib/x86_64-linux-gnu/libthread_db.so.1". ["charlie" "foxtrot"
"oscar" "echo" "alpha" "golf" "tango" "delta" "bravo"] ["alpha"
"bravo" "charlie" "delta" "echo" "foxtrot" "golf" "oscar" "tango"] check_array OK
Program received signal SIGSEGV, Segmentation fault.
__strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:283
283 ../sysdeps/x86_64/multiarch/strcmp-avx2.S: No such file
or directory. (gdb) bt
#0 __strcmp_avx2 () at
../sysdeps/x86_64/multiarch/strcmp-avx2.S:283 #1
0x00005555555553e0 in mystrcmp (s=0x555555556030, t=0x7fffffffde10)
at mycmpstr.c:50 #2 0x00007ffff7e0a53c in __GI_bsearch (__key=0x555555556030, __base=0x7fffffffddf0, __nmemb=<optimized
, __size=8, __compar=0x5555555553b7 <mystrcmp>) at out>../bits/stdlib-bsearch.h:33 #3 0x0000555555555317 in main ()
at mycmpstr.c:30
The difference is that without BUG defined I use my own binary
search, but with BUG defined I use bsearch.
[...]
int mystrcmp(const void* s, const void* t) {
return strcmp(*(const char**)s, *(const char**)t);
You don't need to cast void*s, change that to:
return strcmp(s, t);
}
The elements of the words array have type pointer-to-char.
So the first argument to bsearch should be the address of such an
element, that is,
a pointer-to-pointer-to-char and it should contain the adress of a
pointer to the first character of the oscar string.
Also, the value returned from bsearch should be interpreted as a pointer-to-pointer-to-char.
char * key = "oscar";
char * * p = bsearch(&key, words, size, sizeof(char*), mystrcmp);
index = p - words[0];
found = p != NULL:
Two problems here: first, if bsearch returns NULL, the subtraction is ill-defined.
Second, if bsearch returns non-null the index will be p - words, not p - words[0];
found = p != NULL:
if (found) index = p - words;
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 02:18:54 |
Calls: | 10,387 |
Calls today: | 2 |
Files: | 14,061 |
Messages: | 6,416,752 |