Seg Fault

Discussion in 'C Programming' started by Digital Khaos, Aug 14, 2003.

  1. Hello all,

    I've been watching the group now for a bit, and I'm still not sure
    if this question is relelvent to this group, but here goes anyway:

    The following code:

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

    int main(void)
    {
    char *string = "//usr//bin//emacs";

    strcat(string, " &");
    system(string);

    return(0);
    }

    is throwing a seg fault. Can anyone tell me why?

    Sorry if this is off topic.
     
    Digital Khaos, Aug 14, 2003
    #1
    1. Advertising

  2. Digital Khaos wrote:
    > Hello all,
    >
    > I've been watching the group now for a bit, and I'm still not sure
    > if this question is relevant to this group, but here goes anyway:
    >
    > The following code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >

    int main(int argc, char* argv[]) {
    >
    > char string[20] = "//usr//bin//emacs";
    >
    > strcat(string, " &");
    > system(string);
    >
    > return 0;
    > }
    >
    > is throwing a seg fault. Can anyone tell me why?


    Note my revision above.
    You need to reserve enough space in your string array
    to append two characters.
     
    E. Robert Tisdale, Aug 14, 2003
    #2
    1. Advertising

  3. Digital Khaos <> writes:

    > The following code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > char *string = "//usr//bin//emacs";
    >
    > strcat(string, " &");
    > system(string);
    >
    > return(0);
    > }
    >
    > is throwing a seg fault. Can anyone tell me why?


    `string' is pointing to a string literal. You cannot modify a string literal
    or append anything to it. The `strcat' function expects that its first
    argument points to a buffer large enough to hold resulting string. You
    must provide such a buffer. This is one way to do it:


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

    int main (void)
    {
    const char *const command = "/usr/bin/emacs";
    const char *const bg = " &";
    char *buffer;

    /* `buffer' should be large enough to hold the concatenated
    string, including the terminating '\0' character. */
    buffer = malloc (strlen (command) + strlen (bg) + 1);

    if (buffer != NULL)
    {
    strcpy (buffer, command);
    strcat (buffer, bg);
    system (buffer);
    free (buffer);
    }
    else
    {
    fputs ("memory allocation failed\n", stderr);
    return EXIT_FAILURE;
    }

    return 0;
    }


    Martin
     
    Martin Dickopp, Aug 14, 2003
    #3
  4. Digital Khaos

    Default User Guest

    Digital Khaos wrote:
    >
    > Hello all,
    >
    > I've been watching the group now for a bit, and I'm still not sure
    > if this question is relelvent to this group, but here goes anyway:
    >
    > The following code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > char *string = "//usr//bin//emacs";
    >
    > strcat(string, " &");
    > system(string);
    >
    > return(0);
    > }
    >
    > is throwing a seg fault. Can anyone tell me why?
    >
    > Sorry if this is off topic.



    1. You can't modify string literals.

    2. There's no place to store the characters you want to add to the
    string.

    3. http://www.eskimo.com/~scs/C-faq/top.html



    Brian Rodenborn
     
    Default User, Aug 14, 2003
    #4
  5. Digital Khaos <> wrote in
    news:Xns93D78EFAEB2B5spamdigitalkhaosnet@68.6.19.6:

    > The following code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > char *string = "//usr//bin//emacs";
    >
    > strcat(string, " &");
    > system(string);
    >
    > return(0);
    > }
    >
    > is throwing a seg fault. Can anyone tell me why?
    >
    > Sorry if this is off topic.


    Thanks, I totally see and understand my mistake, works now
    appreciate it.
     
    Digital Khaos, Aug 14, 2003
    #5
  6. Groovy hepcat Digital Khaos was jivin' on Thu, 14 Aug 2003 21:03:19
    GMT in comp.lang.c.
    Seg Fault's a cool scene! Dig it!

    > I've been watching the group now for a bit, and I'm still not sure
    >if this question is relelvent to this group, but here goes anyway:
    >
    >The following code:
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >int main(void)
    >{
    > char *string = "//usr//bin//emacs";


    The above string is apparently wrong. You seem to be confusing
    forward slashes (/) with backslashes (\) which are used to introduce a
    character escape sequence in a character constant or string literal.
    Because of this, backslashes themselves need to be escaped in
    character constants and string literals to produce literal backslash
    characters, but forward slashes do not.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Aug 18, 2003
    #6
  7. (Peter "Shaggy" Haywood) writes:
    > Groovy hepcat Digital Khaos was jivin' on Thu, 14 Aug 2003 21:03:19
    > GMT in comp.lang.c.
    > Seg Fault's a cool scene! Dig it!

    [...]
    > > char *string = "//usr//bin//emacs";

    >
    > The above string is apparently wrong. You seem to be confusing
    > forward slashes (/) with backslashes (\) which are used to introduce a
    > character escape sequence in a character constant or string literal.
    > Because of this, backslashes themselves need to be escaped in
    > character constants and string literals to produce literal backslash
    > characters, but forward slashes do not.


    <OT>
    But it's likely to work anyway. On systems with Unix-like file
    systems, "//usr//bin//emacs" is typically equivalent to
    "/usr/bin/emacs".
    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Aug 19, 2003
    #7
    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. Vinod

    Suprising seg fault!!!

    Vinod, Sep 27, 2003, in forum: C++
    Replies:
    3
    Views:
    483
    Ron Natalie
    Sep 27, 2003
  2. Corrine
    Replies:
    1
    Views:
    440
    Gianni Mariani
    Nov 27, 2003
  3. Manuel Maria Diaz Gomez

    fstream::open & seg fault

    Manuel Maria Diaz Gomez, Jul 20, 2004, in forum: C++
    Replies:
    3
    Views:
    1,275
    John Harrison
    Jul 20, 2004
  4. goktan
    Replies:
    4
    Views:
    2,033
    Howard
    Aug 2, 2004
  5. Matthias Kaeppler

    seg-fault on vector-auto-reserve

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    2
    Views:
    479
    Victor Bazarov
    Feb 27, 2005
Loading...

Share This Page