segmentation fault

Discussion in 'C Programming' started by dough, Oct 3, 2005.

  1. dough

    dough Guest

    Can anyone tell me why I get a segmentation fault with the following
    code and call?

    char *strlwr(char *s)
    {
    while( s != NULL && *s != '\0' )
    {
    *s = tolower(*s);
    s++;
    }

    return s;
    }

    printf("%s", strlwr("HAPPY"));
     
    dough, Oct 3, 2005
    #1
    1. Advertising

  2. >Can anyone tell me why I get a segmentation fault with the following
    >code and call?


    Attempting to write on a character in a quoted string constant
    invokes the wrath of undefined behavior. The compiler is
    permitted to place such data in ROM or memory where write is
    not allowed.

    >printf("%s", strlwr("HAPPY"));


    Gordon L. Burditt
     
    Gordon Burditt, Oct 3, 2005
    #2
    1. Advertising

  3. "dough" <> wrote in news:1128301233.146582.222730
    @g14g2000cwa.googlegroups.com:

    > Can anyone tell me why I get a segmentation fault with the following
    > code and call?
    >
    > char *strlwr(char *s)
    > {
    > while( s != NULL && *s != '\0' )
    > {
    > *s = tolower(*s);
    > s++;
    > }
    >
    > return s;
    > }
    >
    > printf("%s", strlwr("HAPPY"));


    This is a FAQ:

    http://www.eskimo.com/~scs/C-faq/q16.6.html

    Also, functions with the str prefix are in reserved by the standard.

    Note that the function above will return a pointer to the terminating nul
    character.

    #include <ctype.h>
    #include <stdio.h>

    char *mystrlwr(char *s) {
    if (s) {
    char *t = s;
    while (*t) {
    *t = tolower((unsigned char) *t);
    ++t;
    }
    }
    return s;
    }

    int main(void) {
    char s[] = "HELLO";
    printf("%s\n", mystrlwr(s));
    return 0;
    }

    D:\Home> gcc -Wall s.c -o s.exe

    D:\Home> s
    hello

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)
     
    A. Sinan Unur, Oct 3, 2005
    #3
  4. dough

    dough Guest

    thank you for that. it cleared a few of my questions.

    what happens if i need s to be a pointer because i don't know how big
    the string is:

    char *s;
    while( fscanf(f, "%s", s) != EOF )
    {
    printf("%s", mystrlwr(s));
    }

    Is there some kind of malloc() that i need to do as well?
     
    dough, Oct 3, 2005
    #4
  5. It's not the function, it's that you return and address that is past the end
    of the string.

    dough wrote:

    > Can anyone tell me why I get a segmentation fault with the following
    > code and call?
    >
    > char *strlwr(char *s)
    > {
    > while( s != NULL && *s != '\0' )
    > {
    > *s = tolower(*s);
    > s++;
    > }
    >
    > return s;
    > }
    >
    > printf("%s", strlwr("HAPPY"));


    --
    Remove '.nospam' from e-mail address to reply by e-mail
     
    James McIninch, Oct 3, 2005
    #5
  6. dough

    uououo Guest

    #include <stdio.h>
    #include <ctype.h>

    char *strlwr(char *s)
    {
    if( s ){
    char *t=s;
    for(; *t; ++t)
    ( *t >= 'A' && *t <= 'Z' ? *t-='A'-'a':0 );
    }
    return s;
    }

    int main()
    {
    char array[]="HAPPY123";
    printf("%s", strlwr(array));

    return 0;
    }
     
    uououo, Oct 3, 2005
    #6
  7. dough

    Artie Gold Guest

    James McIninch wrote:
    [top posting corrected]
    >
    > dough wrote:
    >>Can anyone tell me why I get a segmentation fault with the following
    >>code and call?
    >>
    >>char *strlwr(char *s)
    >>{
    >> while( s != NULL && *s != '\0' )
    >> {
    >> *s = tolower(*s);
    >> s++;
    >> }
    >>
    >> return s;
    >>}
    >>
    >>printf("%s", strlwr("HAPPY"));

    >
    >
    > It's not the function, it's that you return and address that is past

    the end
    > of the string.
    >


    Erm, no. See elsethread. [It's in the attempt to modify a string literal
    -- invoking undefined behavior.] Besides, the returned pointer would be
    pointing to the terminating null character of the string. Even
    derefencing it would be OK (assuming it *was*, in fact a proper C string).

    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Oct 3, 2005
    #7
  8. dough wrote:
    > Can anyone tell me why I get a segmentation fault with the following
    > code and call?


    Because you are trying to modify a string literal.
    (You are also improperly invading the implementation's namespace by
    using the identifier 'strlwr' to name your function. Stop it.)

    This question and variations on it have been asked so often that
    whenever it is asked it signals that the questioner has not bothered to
    follow simple usenet etiquette. It is expected that a civilized poster
    to usenet will have
    a) checked the FAQ before posting,
    b) followed the newsgroup before posting,
    c) checked the archives before posting.
    That you could ask your question shows that you have done *none* of the
    above.


    >
    > char *strlwr(char *s)
    > {
    > while( s != NULL && *s != '\0' )
    > {
    > *s = tolower(*s);
    > s++;
    > }
    >
    > return s;
    > }
    >
    > printf("%s", strlwr("HAPPY"));
    >
     
    Martin Ambuhl, Oct 3, 2005
    #8
  9. dough

    uououo Guest

    you're right :)
     
    uououo, Oct 3, 2005
    #9
  10. "dough" <> wrote in news:1128304016.781727.253100
    @z14g2000cwz.googlegroups.com:

    > thank you for that.


    Thank who for what? Please quote an appropriate amount of context.

    > it cleared a few of my questions.
    >
    > what happens if i need s to be a pointer because i don't know how big
    > the string is:


    Are you trying to get the whole program written by others?

    > char *s;
    > while( fscanf(f, "%s", s) != EOF )
    > {
    > printf("%s", mystrlwr(s));
    > }
    >
    > Is there some kind of malloc() that i need to do as well?


    1. s is pointing to garbage.
    2. But more importantly, there is no amount of memory you can allocate
    for s that would not be susceptible to being overflowed.

    In the case of dealing with whole files, I would be inclined to keep
    things simple, and use fgetc.

    Sinan

    PS: Please read the whole FAQ in its entirety at least once.

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)
     
    A. Sinan Unur, Oct 3, 2005
    #10
  11. This is a bit off the track the rest of the ppl in this thread are on.
    However I think this is useful.
    For any case of "Segmentation fault" , compile the prog with (in gcc)
    with
    -Wall (enables all warnings) and -g (generates debug info) switches
    enabled.
    Then run the executable under gdb.
    That usually pinpoints the source of the segFault.
    Regards,
    Frodo Baggins
    Gordon Burditt wrote:
    > >Can anyone tell me why I get a segmentation fault with the following
    > >code and call?

    >
    > Attempting to write on a character in a quoted string constant
    > invokes the wrath of undefined behavior. The compiler is
    > permitted to place such data in ROM or memory where write is
    > not allowed.
    >
    > >printf("%s", strlwr("HAPPY"));

    >
    > Gordon L. Burditt
     
    Frodo Baggins, Oct 3, 2005
    #11
  12. dough

    Flash Gordon Guest

    Frodo Baggins wrote:

    Please don't top post. Your reply belongs *under* the text you are
    replying to.

    > This is a bit off the track the rest of the ppl in this thread are on.


    Please don't use contractions like ppl, they make your post harder to
    read for no good reason.

    > However I think this is useful.
    > For any case of "Segmentation fault" , compile the prog with (in gcc)
    > with
    > -Wall (enables all warnings) and -g (generates debug info) switches
    > enabled.


    <OT>
    You should also use "-ansi -pedantic -O" with gcc and some people
    recommend -W as well.
    <OT>

    There are GNU mailing lists where you can get more help on this
    including the reason for adding -O.

    However, there was no indication in what you quoted that the OP was
    using gcc. I've used a number of other compilers under systems that can
    produce a "segmentation fault".

    > Then run the executable under gdb.
    > That usually pinpoints the source of the segFault.


    This only works some of the time, undefined behaviour can often lead to
    a crash a long way down the line such as when freeing memory.

    You should also redirect people to other groups when you start
    discussing the specifics of particular systems since they are not on
    topic here beyond the odd example to illustrate a point.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Oct 3, 2005
    #12
  13. dough

    Joe Wright Guest

    dough wrote:
    > Can anyone tell me why I get a segmentation fault with the following
    > code and call?
    >
    > char *strlwr(char *s)
    > {
    > while( s != NULL && *s != '\0' )
    > {
    > *s = tolower(*s);
    > s++;
    > }
    >
    > return s;
    > }
    >
    > printf("%s", strlwr("HAPPY"));
    >


    First, the anonymously defined array "HAPPY" is not guaranteed
    writeable. The segfault might be in the strlwr() function.

    But then you return s from strlwr(). s points to the terminating NUL of
    "HAPPY" (or maybe "happy"). What do you expect printf() to do with that?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Oct 3, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    871
  2. Pud
    Replies:
    0
    Views:
    578
  3. Replies:
    0
    Views:
    532
  4. Ivan Vecerina
    Replies:
    0
    Views:
    488
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,623
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page