By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,493 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,493 IT Pros & Developers. It's quick & easy.

Segmentation fault with strcmp

P: 7
This is on a RedHat 7.3, gcc 2.96 (old, I know). You should be able to copy/paste following code to confirm that it crashes at strcmp in function scmp even though the pointers appear to be valid (the print statements give the expected output). Well, evidently they aren't, but what's wrong?

#include <stdio.h>
#include <string.h>

#define MAXLEN 20

static int scmp( const void *sp1, const void *sp2 );

main()
{
char (*stringarr)[MAXLEN];

if (( stringarr = malloc(MAXLEN*3*sizeof(char)) ) == NULL)
{
fprintf(stderr,"Memory allocation failure\n");
exit(-1);
}

sprintf(stringarr[0],"2001062600aa.fit");
sprintf(stringarr[1],"2001062600aa.fit");
sprintf(stringarr[2],"2001062602e.fit");

qsort(stringarr, 3, MAXLEN*sizeof(char), scmp);

}

static int scmp( const void *sp1, const void *sp2 )
{
fprintf(stderr,"String 1: %s\n", sp1);
fprintf(stderr,"String 2: %s\n", sp2);
return( strcmp(*(char **)sp1, *(char **)sp2) );
fprintf(stderr,"Crashed before this\n");
}
Sep 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: 7
Whereas I still don't know what's wrong with the pointers I did find a workaround, namely to use strcmp directly in qsort:

Expand|Select|Wrap|Line Numbers
  1. qsort(stringarr, 3, MAXLEN*sizeof(char), (int(*)(const void*, const void*))&strcmp);
Works for me.
Sep 9 '06 #2

P: 61
Can you explain me the intent of the following statement:

return( strcmp(*(char **)sp1, *(char **)sp2) );


See the Man page of strcamp.
Sep 11 '06 #3

P: 7
I've seen the man page of strcmp; did not help me. The explanation for the compare function is here:
http://www.lysator.liu.se/c/c-faq/c-12.html
I still don't see why it won't work. If you can tell I would like to know the answer.
Sep 18 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
This is wrong

qsort(stringarr, 3, MAXLEN*sizeof(char), scmp);

stringarr is an array of char * each element is sizeof(char *) bytes but you have declared each element as being MAXLEN*sizeof(char) bytes in length. This should be

qsort(stringarr, 3, sizeof(char *), scmp);



static int scmp( const void *sp1, const void *sp2 )
{
fprintf(stderr,"String 1: %s\n", sp1);
fprintf(stderr,"String 2: %s\n", sp2);
return( strcmp(*(char **)sp1, *(char **)sp2) );
}

This is passed pointers to elements or pointers for you this is effectively equivilent to &stringarr[N] where 0<=N<3. This has the type char * but you have cast it to char ** then dereferenced it. This is way using strcmp directly works because it doesn't do this extra level of indirection

return( strcmp(*(char **)sp1, *(char **)sp2) );

should be

return( strcmp((char *)sp1, (char *)sp2) );

somehow you new this for your fprintf statements but not for the strcmp !
Sep 18 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.