Tech Support Forum banner
Status
Not open for further replies.
1 - 3 of 3 Posts

·
Registered
Joined
·
2 Posts
Discussion Starter · #1 ·

I'm teaching myself how to use gcc. I've written a simple function and a main program to test it. They both seem to compile clean, but when I run the test program it gets a segfault. If I run it with gdb I see an error message, but I'm not sure what it's trying to tell me.
Here's the function:


/* isABBRev - Compares two strings, returning 1 if the second string is an */
/* abbreviation of the first string, otherwise 0. */
/* */
/* Synopsis: int isABBRev(char *STRing1, char *string2); */
/* */
/* The minimum number of characters that must match is determined */
/* by the number of leading uppercase characters in the first */
/* string. If all characters of the second string match the */
/* leftmost characters of the first string (regardless of case), */
/* the second string is an abbreviation of the first string. */

#include <ctype.h>
#include <string.h>
#define ARBMAX 50

int isABBRev(char* fullWord, char* testWord)
{int minLength = 1;
int rc;
char *c;

/* Count leading uppercase characters to determine the minimum abbreviation. */
c = fullWord;

while (!islower(c))
{minLength++;
c++;
};

if (strlen(testWord) > ARBMAX) /* abbreviation is too long. */
return(0);

if (strlen(testWord) > strlen(fullWord)) /* abbreviation is too long. */
return(0);

if (strlen(testWord) < minLength) /* abbreviation is too short. */
return(0);

rc = strncasecmp(testWord, fullWord, strlen(testWord));
return(rc == 0);
}


Here's the test program:


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

int isABBRev(char*, char*)
/* ------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{int rc;

if (argc != 3)
{printf("Usage: testABBRev: FULLword abbreviation\n");
printf(" where the uppercase part of FULLword indicates\n");
printf(" the minimum abbreviation length allowed.\n");
return 12;
}
rc = isABBRev(argv[1], argv[2]);

if (rc == 0)
{printf("%s is not an abbreviation of %s.\n", argv[2], argv[1]);
return 4;
}

else
{printf("%s is an abbreviation of %s.\n", argv[2], argv[1]);
return 0;
}
}


Compile commands look like this:


gcc -Wall -c -g isABBRev.c testABBRev.c
gcc -Wall -o testABBRev isABBRev.o testABBRev.o


Neither of them produce any error messages.
Execution of testABBRev looks like this:


./testABBRev HISTory hist
Segmentation fault


Execution via gdb shows this:


gdb testABBRev
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-suse-linux"...
(gdb) break main
Breakpoint 1 at 0x80485bc: file testABBRev.c, line 10.
(gdb) run HISTory history
Starting program: /home/turriff/Documents/Code/Linux-pipelines/testABBRev HISTory history

Breakpoint 1, main (argc=3, argv=0xbfc8ad54) at testABBRev.c:10
10 if (argc != 3)
(gdb) step
16 rc = isABBRev(argv[1], argv[2]);
(gdb) step
isABBRev (fullWord=0xbfc8b319 "HISTory", testWord=0xbfc8b321 "history")
at isABBRev.c:17
17 {int minLength = 1;
(gdb) step
22 c = fullWord;
(gdb) step
24 while (!islower(c))
(gdb) step
__ctype_b_loc () at ../include/ctype.h:27
27 ../include/ctype.h: No such file or directory.
in ../include/ctype.h
(gdb) finish
Run till exit from #0 __ctype_b_loc () at ../include/ctype.h:27
0x080484f7 in isABBRev (fullWord=0xbfc8b319 "HISTory",
testWord=0xbfc8b321 "history") at isABBRev.c:24
24 while (!islower(c))
Value returned is $1 = (const short unsigned int **) 0xb7f0468c
(gdb) step

Program received signal SIGSEGV, Segmentation fault.
0x08048501 in isABBRev (fullWord=0xbfc8b319 "HISTory",
testWord=0xbfc8b321 "history") at isABBRev.c:24
24 while (!islower(c))
(gdb)
:


According to man isupper (which lumps a bunch of function descriptions together):


ISALPHA(3) Linux Programmer's Manual ISALPHA(3)



NAME
isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, islower,
isprint, ispunct, isspace, isupper, isxdigit - character classification
routines

SYNOPSIS
#include <ctype.h>

int isalnum(int c);
int isalpha(int c);
int isascii(int c);
int isblank(int c);
int iscntrl(int c);
int isdigit(int c);
int isgraph(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isspace(int c);
int isupper(int c); <====
int isxdigit(int c);
:


isupper() should return an int, but gdb says it's returning a const short unsigned int. Is this what is causing the segfault? If so, what should I do to correct the problem?

Leslie

BTW, if this is not the right place to ask such a question, can you recommend another place?

 

·
Alternative Computing, Moderator
Joined
·
2,754 Posts
I'm teaching myself how to use gcc. I've written a simple function and a main program to test it. They both seem to compile clean, but when I run the test program it gets a segfault. If I run it with gdb I see an error message, but I'm not sure what it's trying to tell me.
--snip--
__ctype_b_loc () at ../include/ctype.h:27
27 ../include/ctype.h: No such file or directory.
in ../include/ctype.h
--snip--
Leslie

BTW, if this is not the right place to ask such a question, can you recommend another place?
Install the gcc dev package which contains the development headers.
Suse is a great linux but I've also had programs fail becuse the development packages were not installed.

Linux questions has a forum on programming also a good place to ask or seach for C forums or C+ etc on google
hope that helps
H8k
 

·
Registered
Joined
·
2 Posts
Discussion Starter · #3 ·
As far as I can tell from listing /usr/include/ and reviewing YaST, the appropriate headers are present. YaST shows:

i │glib-devel
i │glib2-devel
i │glibc-devel

I will see what LinuxQuestions, etc. has. Thanks.
 
1 - 3 of 3 Posts
Status
Not open for further replies.
Top