[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C program segfaults when run, not under GDB
- To: milesteg(at-nospam)surfree.net.il
- Subject: Re: C program segfaults when run, not under GDB
- From: Adi Stav <stav(at-nospam)actcom.co.il>
- Date: Sun, 25 Nov 2001 15:46:07 +0200
- Cc: linux-il(at-nospam)linux.org.il
- Delivered-To: linux.org.il-linux-il@linux.org.il
- In-Reply-To: <3C00BBB9.4060905@bezeqint.net>; from sf_milesteg@bezeqint.net on Sun, Nov 25, 2001 at 11:36:57AM +0200
- Mail-Followup-To: milesteg@surfree.net.il, linux-il@linux.org.il
- References: <3C00BBB9.4060905@bezeqint.net>
- Sender: Adi Stav <adi(at-nospam)stav.org.il>
- Sender: linux-il-bounce(at-nospam)cs.huji.ac.il
- User-Agent: Mutt/1.2.5i
On Sun, Nov 25, 2001 at 11:36:57AM +0200, Eugene Romm wrote:
> Hello.
>
> I've written a procedure that's supposed to remove all occurances of
> string2 from string1 (parameters).
> For reasons I do not understand, the program compiles but segfaults when
> run from the command prompt, but silently executes without a warning
> when run under GDB. Attached is the program. Segfault occurs on line 29,
> as far as I can tell.
> #include <stdio.h>
> #define yes 1
> #define no 0
>
> main() {
> squeezechar2("abcdeFghijklmnopqrstuvwxyZ","deF");
> }
>
> squeezechar2(s1,s2)
> char s1[];
> char s2[];
> {
> int c1,c2;
> int s2length;
> int factor=0;
> int moo;
> int matches=no;
>
> for (s2length=0;s2[s2length]!='\0';s2length++); // find out s2length
>
> for (c1=c2=0;s1[c1] != '\0' ; c1++) { // loop to end of string
> if (s1[c1]==s2[c2]) { // if first char matches
> for (matches=yes,moo=0;(moo<s2length) && (s1[c1+moo]==s2[moo]);moo++);
> if (moo!=s2length) matches=no;
> else
> factor+=s2length; // factor changes if there's a match
> }
> // printf("%c",s1[c1]);
> if (factor!=0) s1[c1]=s1[c1+factor];
> }
> }
String literals are constant. Modifying them will lead to undefined
behavior, which might as well the reason for your segfault.
Also: (shouldn't lead to segfault)
- You appear to be writing K&R C, so C++-style comments are illegal.
- The code is not portable to newer compilers because the functions
have no return values. K&R allows functions that explicitly return
int, so you might want to use that option.
- main() does not return success, which could mess up programs or
scripts trying to check the program's status.
=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il