NO

Author Topic: bsearch  (Read 11427 times)

tpekar

  • Guest
bsearch
« on: March 08, 2013, 11:12:07 PM »
I am having trouble getting the following program to run.  I know I am doing something wrong but can't figure out what it is.  It compiles and runs the qsort but crashes when it tries to do the bsearch.  Can someone help?  Thanks!

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char source[129]="";
char *dta[5]={"muid","epicsid","ssn","inst","chkdat"};
char *rec[200];
char input[256];
char key[10];
char *ans;
int i,e;
int main(int argc, char *argv[]) {
int strcmp();
int compare(const void *arg1, const void *arg2);
for (i=0;i<5;i++) {
  strcpy(input,dta[i]);
  rec[i]=malloc(strlen(input)+1);
  strcpy(rec[i],input);
}
qsort((void *)rec,(size_t)i,sizeof(char *),compare);
e=i;
for (i=0;i<e;i++) printf("%s\n",rec[i]);
strcpy(key,"inst");
ans=bsearch(key,(void *)rec,(size_t)e,sizeof(char *),compare);
if (ans!=NULL) printf("\nfound inst");
strcpy(key,"inst1");
ans=bsearch(key,(void *)rec,(size_t)e,sizeof(char *),compare);
if (ans==NULL) printf("\ndid not find inst1");
}

int compare(const void *arg1, const void *arg2) {
return strcmp(* (char**) arg1, * (char**) arg2);
}
« Last Edit: March 09, 2013, 09:46:57 AM by Stefan Pendl »

CLR

  • Guest
Re: bsearch
« Reply #1 on: March 08, 2013, 11:36:59 PM »
You can read the error messages emitted by the compiler.

CommonTater

  • Guest
Re: bsearch
« Reply #2 on: March 09, 2013, 02:23:35 AM »
I am having trouble getting the following program to run.  I know I am doing something wrong but can't figure out what it is.  It compiles and runs the qsort but crashes when it tries to do the bsearch.  Can someone help?  Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char source[129]="";
char *dta[5]={"muid","epicsid","ssn","inst","chkdat"};
char *rec[200];
char input[256];
char key[10];
char *ans;
int i,e;
int main(int argc, char *argv[]) {
int strcmp();
int compare(const void *arg1, const void *arg2);
for (i=0;i<5;i++) {
  strcpy(input,dta);
  rec=malloc(strlen(input)+1);
  strcpy(rec,input);
}
qsort((void *)rec,(size_t)i,sizeof(char *),compare);
e=i;
for (i=0;i<e;i++) printf("%s\n",rec);
strcpy(key,"inst");
ans=bsearch(key,(void *)rec,(size_t)e,sizeof(char *),compare);
if (ans!=NULL) printf("\nfound inst");
strcpy(key,"inst1");
ans=bsearch(key,(void *)rec,(size_t)e,sizeof(char *),compare);
if (ans==NULL) printf("\ndid not find inst1");
}

int compare(const void *arg1, const void *arg2) {
return strcmp(* (char**) arg1, * (char**) arg2);
}

You mean other than mismatched brackets, typecasting for no reason at all and code that's so poorly formatted as to be almost impossible to follow?

JohnF

  • Guest
Re: bsearch
« Reply #3 on: March 09, 2013, 08:42:34 AM »
For anyone who doesn't know Pelle provides a C formatting feature.

Highlight the section of code that needs formatting, then from the menu - Source/Convert to/Formatted C code

John

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: bsearch
« Reply #4 on: March 09, 2013, 09:51:50 AM »
It compiles and runs the qsort but crashes when it tries to do the bsearch.

Hi tpekar,

Which options did you use? I get some compile errors, see below.

Your wording suggests that you can compile the code, but the executable crashes. Does it throw an exception? The easiest way to test why would be to launch a debugger (e.g. Olly) and see where it chokes.

Code: [Select]
Building qSortCrash.obj.
qSortCrash.c(15): error #2140: Type error in argument 2 to 'strcpy'; expected 'const char * restrict' but found 'char * *'.
qSortCrash.c(16): error #2168: Operands of '=' have incompatible types 'char * [200]' and 'void *'.
qSortCrash.c(16): error #2088: Lvalue required.
qSortCrash.c(17): error #2140: Type error in argument 1 to 'strcpy'; expected 'char * restrict' but found 'char * *'.
qSortCrash.c(21): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'char *' but found 'char * *'.

CommonTater

  • Guest
Re: bsearch
« Reply #5 on: March 09, 2013, 10:18:41 AM »
Your wording suggests that you can compile the code, but the executable crashes. Does it throw an exception? The easiest way to test why would be to launch a debugger (e.g. Olly) and see where it chokes.

Pelles C has a built in source level debugger... no need for external tools.
 

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: bsearch
« Reply #6 on: March 09, 2013, 11:47:31 AM »
It compiles and runs the qsort but crashes when it tries to do the bsearch.  Can someone help?
Code: [Select]
...
char *pkey = key;
ans = bsearch(&pkey, (void *)rec, (size_t)e, sizeof(char *), compare);
...
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: bsearch
« Reply #7 on: March 09, 2013, 04:03:25 PM »
Pelles C has a built in source level debugger... no need for external tools.

You mean the one that shows the flags as 00000AC3? Hmmmm...

CommonTater

  • Guest
Re: bsearch
« Reply #8 on: March 09, 2013, 04:10:31 PM »
Pelles C has a built in source level debugger... no need for external tools.

You mean the one that shows the flags as 00000AC3? Hmmmm...

No... I mean the one that if used correctly can take you right to the line of source code that's causing your problems.
 
 

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: bsearch
« Reply #9 on: March 09, 2013, 04:25:06 PM »
No... I mean the one that if used correctly can take you right to the line of source code that's causing your problems.

That one is called Olly, and among many other features displays the flags in a useful format (CZS etc, red if set, black if clear) and in a readable font.

CommonTater

  • Guest
Re: bsearch
« Reply #10 on: March 09, 2013, 04:45:29 PM »
No... I mean the one that if used correctly can take you right to the line of source code that's causing your problems.

That one is called Olly, and among many other features displays the flags in a useful format (CZS etc, red if set, black if clear) and in a readable font.

I have OllyDebug here... it's mostly useless in C projects.
 
Seriously... We need to correlate problems to lines of source code, not some silly dissassembly listing. Since we don't have direct access to the flags register in C --and don't need it-- there's no reason to care about how it's displayed.
 
Do you know how to use the internal debugger? 
 
  • Take any working project you have
  • Load it into the IDE...
  • Right click on the project name in the tree and select "Project Options".
  • On the compiler tab select  Debug Information = Full.
  • On the linker tab... for 32 bit projects select  Debug Information  = CodeView & Coff... for 64 bit projects select Debug Information  = Codeview.
  • Click OK to save the settings.
Now, on the main toolbar click the "Go Debug" button....
If you can't see how that is more useful to a C programmer, you're a complete idiot. 
 
« Last Edit: March 09, 2013, 04:48:20 PM by CommonTater »

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: bsearch
« Reply #11 on: March 09, 2013, 07:50:51 PM »
If you can't see how that is more useful to a C programmer, you're a complete idiot.

Lovely ;D

Giovanbattista

  • Guest
Re: bsearch
« Reply #12 on: March 09, 2013, 08:11:43 PM »
Most of the times PellesC Debugger is more than enough
for the task.

G.

CommonTater

  • Guest
Re: bsearch
« Reply #13 on: March 09, 2013, 08:51:40 PM »
Most of the times PellesC Debugger is more than enough
for the task.

Hi Giovan...
You need to take into account where this is coming from...
 
Our friend JJ has it in his head that he's out to vanquish me for some wholly imagined slight on a different forum and thinks (wrongly) that he can come over here and show everyone how smart he is by going after me. In the end all he's doing is giving the people here some really BAD advice that I (and others) then have to try and fix.
 
Indeed Pelle's debugger is perfectly adequate for almost all C programming tasks. 
It is afterall designed for exactly that purpose.
 
« Last Edit: March 09, 2013, 10:20:55 PM by CommonTater »

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: bsearch
« Reply #14 on: March 09, 2013, 11:02:11 PM »
Most of the times PellesC Debugger is more than enough for the task.

It's not bad, actually. Probably just a question of habit, although I'd be grateful to know how the font could be changed in the register window, it's just unreadable.

Re Tater: He is just a bit upset that his UTF-8 library works with English text only  ;)