473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Segmentation fault with strcmp

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
4 8957
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
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

4
by: Joel | last post by:
I have this bug that quite puzzled me. Basically I am having a segmentation fault on deleting an object, which belongs to a class which is the result of multiple inheritance from two other classes....
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
0
by: jgarber | last post by:
Hello, I just upgraded MySQLdb to the 1.2.0 version provided by Redhat Enterprise Linux ES4. At that point I began to get segfaults when importing twisted after MySQLdb, but not before. --...
10
by: Pedro Pinto | last post by:
Hello there. I've created a socket program so exchange some messages with a server. The problem is the following: it compiles well but when running it gives a segmentation error here:...
3
by: hta1984 | last post by:
i'm coding simple version of 'grep' function.i have a code which works in windows but not in linux.can you look at it and help me?: #include <stdio.h> #include <stdlib.h> #include...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
3
by: wastedhello | last post by:
hi im farely new to programming, doing it for university, but i cant figure out why this isnt working. it comes up with segmentation faults. can anyone help #include <stdio.h> #include...
4
by: sid | last post by:
Hi! I am using gcc (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5) on an Ubuntu 6.06 64bit version on an AMD 64bit machine. Each time I compile and execute the following program I get segmentation fault....
22
by: mihiri | last post by:
When I use this code it compile without errors ,but when run it ,it shows segmentation fault.I have general idea about segmentation fault,but I can't identify why it happen in this code.Eventhough I...
6
drumgirl67
by: drumgirl67 | last post by:
I am getting a segmentation fault in a function in a C++ program. "fields" is a two dimensional array that was passed to the function. Each "row" in fields is a 32 character array, and the total...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.