Segmentation Fault (core dumped)

Discussion in 'C Programming' started by DanielJohnson, Feb 18, 2007.

  1. I wrote this small program to reverse each word in the string. For
    example: "I love You" should print as "I evoL uoY". I get Segmentation
    Fault (core dumped) error upon running the program. It compiles fine.

    // Program to reverse each word in the string
    #include<stdio.h>

    int main()
    {
    void reverse_string(char *, int, int);
    char *p = "my name is daniel";
    // keeps count of number is char in the word to reverse
    int count = 0;
    // keeps track of where I started this current word from
    int current_pos = 0;
    // Conitnue till you reach \0
    while(1){
    if (*p == '\0') break;
    count = 0;
    while(*p != ' '){
    if (*p == '\0') break;
    count++;
    p++;
    }
    reverse_string(p, current_pos, count);
    if (*p == ' '){
    p++;
    count++;
    current_pos = count;
    }
    }
    puts(p);
    return 0;
    }

    void reverse_string(char* s, int start, int count){
    int counter = count/2;
    int i = 0;
    while(i < counter){
    *s = *(s-count);
    s--;
    i++;
    }
    }


    Any suggestions......Every help is appreciated.

    Thanks
     
    DanielJohnson, Feb 18, 2007
    #1
    1. Advertising

  2. DanielJohnson

    Zack Guest

    DanielJohnson wrote:

    > // Program to reverse each word in the string
    > #include<stdio.h>
    >
    > int main()
    > {
    > void reverse_string(char *, int, int);
    > char *p = "my name is daniel";
    > // keeps count of number is char in the word to reverse
    > int count = 0;
    > // keeps track of where I started this current word from
    > int current_pos = 0;
    > // Conitnue till you reach \0
    > while(1){
    > if (*p == '\0') break;


    You correctly note that you want to continue until reaching the null
    character in the string.

    However you do not have a null character in your string.
    Try :
    char *p = "my name is daniel\0";

    --
    Zack
     
    Zack, Feb 18, 2007
    #2
    1. Advertising

  3. >I wrote this small program to reverse each word in the string. For
    >example: "I love You" should print as "I evoL uoY". I get Segmentation
    >Fault (core dumped) error upon running the program. It compiles fine.


    You stored the test string in a quoted string literal. C is permitted
    to store such data in a read-only section of the program. If you
    try writing on it, KABOOM!

    Note also that you keep incrementing p, losing track of where the
    beginning of the string is. That's not good when you try to print it.

    >
    >// Program to reverse each word in the string
    >#include<stdio.h>
    >
    >int main()
    >{
    > void reverse_string(char *, int, int);
    > char *p = "my name is daniel";
    > // keeps count of number is char in the word to reverse
    > int count = 0;
    > // keeps track of where I started this current word from
    > int current_pos = 0;
    > // Conitnue till you reach \0
    > while(1){
    > if (*p == '\0') break;
    > count = 0;
    > while(*p != ' '){
    > if (*p == '\0') break;
    > count++;
    > p++;
    > }
    > reverse_string(p, current_pos, count);
    > if (*p == ' '){
    > p++;
    > count++;
    > current_pos = count;
    > }
    > }
    > puts(p);
    > return 0;
    >}
    >
    >void reverse_string(char* s, int start, int count){
    > int counter = count/2;
    > int i = 0;
    > while(i < counter){
    > *s = *(s-count);
    > s--;
    > i++;
    > }
    >}
    >
    >
    >Any suggestions......Every help is appreciated.
    >
    >Thanks
    >
     
    Gordon Burditt, Feb 18, 2007
    #3
  4. On Feb 18, 12:29 am, Zack <> wrote:
    > DanielJohnson wrote:
    > > // Program to reverse each word in the string
    > > #include<stdio.h>

    >
    > > int main()
    > > {
    > > void reverse_string(char *, int, int);
    > > char *p = "my name is daniel";
    > > // keeps count of number is char in the word to reverse
    > > int count = 0;
    > > // keeps track of where I started this current word from
    > > int current_pos = 0;
    > > // Conitnue till you reach \0
    > > while(1){
    > > if (*p == '\0') break;

    >
    > You correctly note that you want to continue until reaching the null
    > character in the string.
    >
    > However you do not have a null character in your string.
    > Try :
    > char *p = "my name is daniel\0";
    >
    > --
    > Zack


    I still get the same error even after putting in \0.
     
    DanielJohnson, Feb 18, 2007
    #4
  5. On Feb 18, 12:31 am, (Gordon Burditt) wrote:
    > >I wrote this small program to reverse each word in the string. For
    > >example: "I love You" should print as "I evoL uoY". I get Segmentation
    > >Fault (core dumped) error upon running the program. It compiles fine.

    >
    > You stored the test string in a quoted string literal. C is permitted
    > to store such data in a read-only section of the program. If you
    > try writing on it, KABOOM!
    >
    > Note also that you keep incrementing p, losing track of where the
    > beginning of the string is. That's not good when you try to print it.
    >
    >
    >
    > >// Program to reverse each word in the string
    > >#include<stdio.h>

    >
    > >int main()
    > >{
    > > void reverse_string(char *, int, int);
    > > char *p = "my name is daniel";
    > > // keeps count of number is char in the word to reverse
    > > int count = 0;
    > > // keeps track of where I started this current word from
    > > int current_pos = 0;
    > > // Conitnue till you reach \0
    > > while(1){
    > > if (*p == '\0') break;
    > > count = 0;
    > > while(*p != ' '){
    > > if (*p == '\0') break;
    > > count++;
    > > p++;
    > > }
    > > reverse_string(p, current_pos, count);
    > > if (*p == ' '){
    > > p++;
    > > count++;
    > > current_pos = count;
    > > }
    > > }
    > > puts(p);
    > > return 0;
    > >}

    >
    > >void reverse_string(char* s, int start, int count){
    > > int counter = count/2;
    > > int i = 0;
    > > while(i < counter){
    > > *s = *(s-count);
    > > s--;
    > > i++;
    > > }
    > >}

    >
    > >Any suggestions......Every help is appreciated.

    >
    > >Thanks


    What should I do prevent KABOOM...I am newbie..give me some tips...
     
    DanielJohnson, Feb 18, 2007
    #5
  6. >> // Program to reverse each word in the string
    >> #include<stdio.h>
    >>
    >> int main()
    >> {
    >> void reverse_string(char *, int, int);
    >> char *p = "my name is daniel";
    >> // keeps count of number is char in the word to reverse
    >> int count = 0;
    >> // keeps track of where I started this current word from
    >> int current_pos = 0;
    >> // Conitnue till you reach \0
    >> while(1){
    >> if (*p == '\0') break;

    >
    >You correctly note that you want to continue until reaching the null
    >character in the string.
    >
    >However you do not have a null character in your string.


    >> char *p = "my name is daniel";

    Yes, there is a null character terminating the above string.

    >Try :
    >char *p = "my name is daniel\0";


    That's just a waste of memory, putting in 2 nulls.
     
    Gordon Burditt, Feb 18, 2007
    #6
  7. On Sat, 17 Feb 2007, Zack wrote:
    >
    > However you do not have a null character in your string.
    > Try :
    > char *p = "my name is daniel\0";


    If you don't know much about C yet, please try not to volunteer
    "answers" to other newbies' questions. You will be wrong, and other
    people in the newsgroup will have to spend extra time un-confusing
    the newbie. Don't worry, there will always be other newbies --- so
    you don't need to jump all over yourself to respond to each one.
    You can afford to wait until you know the language better.

    To the OP: Ignore Zack's post that I just quoted. Your problem
    is with modifying the string literal. Put the string's characters
    in an array first; then you'll be able to change their values.

    -Arthur
     
    Arthur J. O'Dwyer, Feb 18, 2007
    #7
  8. Zack wrote:
    > DanielJohnson wrote:
    >
    >> // Program to reverse each word in the string
    >> #include<stdio.h>
    >>
    >> int main()
    >> {
    >> void reverse_string(char *, int, int);
    >> char *p = "my name is daniel";


    >
    > You correctly note that you want to continue until reaching the null
    > character in the string.
    >
    > However you do not have a null character in your string.
    > Try :
    > char *p = "my name is daniel\0";


    This is truly horrendous advice. You completely wrong about the absence
    of a null character; Daniel's string has one by definition. And that is
    not the problem. He is trying to modify a string literal. This is
    fully covered in the FAQ. Both you and Daniel should read it.

    Daniel needs to declare p not as a pointer to a string literal but to either
    a) declare p as a character array
    or
    b) declare p as a pointer, for which he would then allocate memory
    (via malloc) and to which allocated space he would copy the string
    literal (via strcpy).
    Either of these strategies would give him a modifiable char array.
     
    Martin Ambuhl, Feb 18, 2007
    #8
  9. DanielJohnson

    Zack Guest

    Arthur J. O'Dwyer wrote:
    > If you don't know much about C yet, please try not to volunteer
    > "answers" to other newbies' questions. You will be wrong, and other
    > people in the newsgroup will have to spend extra time un-confusing
    > the newbie. Don't worry, there will always be other newbies --- so
    > you don't need to jump all over yourself to respond to each one.
    > You can afford to wait until you know the language better.

    I will keep quiet.

    --
    Zack
     
    Zack, Feb 18, 2007
    #9
  10. DanielJohnson

    Kelly Guest

    Zack wrote:
    ...snip...
    > I will keep quiet.


    That's much better..you messed up with the earlier question as well!
     
    Kelly, Feb 18, 2007
    #10
  11. On Feb 17, 9:38 pm, "DanielJohnson" <> wrote:
    > On Feb 18, 12:31 am, (Gordon Burditt) wrote:
    >
    > > >I wrote this small program to reverse each word in the string. For
    > > >example: "I love You" should print as "I evoL uoY". I get Segmentation
    > > >Fault (core dumped) error upon running the program. It compiles fine.

    >
    > > You stored the test string in a quoted string literal. C is permitted
    > > to store such data in a read-only section of the program. If you
    > > try writing on it, KABOOM!

    >
    > > Note also that you keep incrementing p, losing track of where the
    > > beginning of the string is. That's not good when you try to print it.

    >
    > > >// Program to reverse each word in the string
    > > >#include<stdio.h>

    >
    > > >int main()
    > > >{
    > > > void reverse_string(char *, int, int);
    > > > char *p = "my name is daniel";
    > > > // keeps count of number is char in the word to reverse
    > > > int count = 0;
    > > > // keeps track of where I started this current word from
    > > > int current_pos = 0;
    > > > // Conitnue till you reach \0
    > > > while(1){
    > > > if (*p == '\0') break;
    > > > count = 0;
    > > > while(*p != ' '){
    > > > if (*p == '\0') break;
    > > > count++;
    > > > p++;
    > > > }
    > > > reverse_string(p, current_pos, count);
    > > > if (*p == ' '){
    > > > p++;
    > > > count++;
    > > > current_pos = count;
    > > > }
    > > > }
    > > > puts(p);
    > > > return 0;
    > > >}

    >
    > > >void reverse_string(char* s, int start, int count){
    > > > int counter = count/2;
    > > > int i = 0;
    > > > while(i < counter){
    > > > *s = *(s-count);
    > > > s--;
    > > > i++;
    > > > }
    > > >}

    >
    > > >Any suggestions......Every help is appreciated.

    >
    > > >Thanks

    >
    > What should I do prevent KABOOM...I am newbie..give me some tips...


    Read the FAQ at http://c-faq.com/
     
    J. J. Farrell, Feb 18, 2007
    #11
  12. DanielJohnson said:

    <snip>

    > What should I do prevent KABOOM...I am newbie..give me some tips...


    Own the memory you use. There's more to it than just making the data
    writeable, however. I changed your program to use writeable data, and
    it still blows up. Here is a version that doesn't. Read, mark, learn,
    and inwardly digest.

    #include <stdio.h> /* note the space between ude and <std */

    /* function prototypes belong at file scope */
    void reverse_substring(char *s,
    char *e);

    int main(void) /* proper declarator for main */
    {
    /* own the data */
    char data[] = "my name is daniel";

    /* keep two pointers - p will track the
    beginning of a word, q the end */

    char *p = data;
    char *q = data;

    /* stop when we hit the end */
    while(*p != '\0')
    {
    /* skip past leading spaces */
    while(*p == ' ')
    {
    ++p;
    }
    q = p;
    while(*q != '\0' && *q != ' ')
    {
    ++q;
    }
    if(q > p)
    {
    /* we've found the end of a word, so reverse it */
    reverse_substring(p, q - 1);

    /* point to start of next word */
    p = q;
    }
    }

    /* write the result */
    puts(data);
    return 0;
    }

    void reverse_substring(char *s,
    char *e)
    {
    char t = 0;

    while(s < e)
    {
    t = *s;
    *s++ = *e;
    *e-- = t;
    }

    /* ain't that a lot simpler? :) */

    return;
    }

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Feb 18, 2007
    #12
  13. "DanielJohnson" <> writes:
    > I wrote this small program to reverse each word in the string. For
    > example: "I love You" should print as "I evoL uoY". I get Segmentation
    > Fault (core dumped) error upon running the program. It compiles fine.

    [snip]
    > char *p = "my name is daniel";

    [snip]
    > reverse_string(p, current_pos, count);


    The comp.lang.c FAQ is at <http://www.c-faq.com/>. You've asked
    question 1.32.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 18, 2007
    #13
  14. DanielJohnson

    Bill Pursell Guest

    On Feb 18, 6:47 am, Zack <> wrote:
    > Arthur J. O'Dwyer wrote:
    > > If you don't know much about C yet, please try not to volunteer
    > > "answers" to other newbies' questions. You will be wrong, and other
    > > people in the newsgroup will have to spend extra time un-confusing
    > > the newbie. Don't worry, there will always be other newbies --- so
    > > you don't need to jump all over yourself to respond to each one.
    > > You can afford to wait until you know the language better.

    >
    > I will keep quiet.



    I don't think that's a good idea. Two proven methods for
    learning any subject are: 1) make mistakes, 2) teach someone
    else. If you combine those two by making errors while you
    try to teach someone, there is only a danger
    if you have no one to correct your mistakes.
    There is no shortage of such people on c.l.c. If you wait
    until you have full mastery of a subject before you try
    to answer someone's question, you will never answer
    anyone's questions.

    It is a good idea, however, to make an effort to ensure
    that your replies are correct. A very good way to
    gauge that you understand something is to post an explanation
    here and see if it gets trounced.

    --
    Bill Pursell
     
    Bill Pursell, Feb 18, 2007
    #14
  15. On Feb 18, 3:08 am, Richard Heathfield <> wrote:
    > DanielJohnson said:
    >
    > <snip>
    >
    > > What should I do prevent KABOOM...I am newbie..give me some tips...

    >
    > Own the memory you use. There's more to it than just making the data
    > writeable, however. I changed your program to use writeable data, and
    > it still blows up. Here is a version that doesn't. Read, mark, learn,
    > and inwardly digest.
    >
    > #include <stdio.h> /* note the space between ude and <std */
    >
    > /* function prototypes belong at file scope */
    > void reverse_substring(char *s,
    > char *e);
    >
    > int main(void) /* proper declarator for main */
    > {
    > /* own the data */
    > char data[] = "my name is daniel";
    >
    > /* keep two pointers - p will track the
    > beginning of a word, q the end */
    >
    > char *p = data;
    > char *q = data;
    >
    > /* stop when we hit the end */
    > while(*p != '\0')
    > {
    > /* skip past leading spaces */
    > while(*p == ' ')
    > {
    > ++p;
    > }
    > q = p;
    > while(*q != '\0' && *q != ' ')
    > {
    > ++q;
    > }
    > if(q > p)
    > {
    > /* we've found the end of a word, so reverse it */
    > reverse_substring(p, q - 1);
    >
    > /* point to start of next word */
    > p = q;
    > }
    > }
    >
    > /* write the result */
    > puts(data);
    > return 0;
    >
    > }
    >
    > void reverse_substring(char *s,
    > char *e)
    > {
    > char t = 0;
    >
    > while(s < e)
    > {
    > t = *s;
    > *s++ = *e;
    > *e-- = t;
    > }
    >
    > /* ain't that a lot simpler? :) */
    >
    > return;
    >
    > }
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
    > email: rjh at the above domain, - www.



    Thanks for such an elegant solution. I learned two things about string
    literal. I apologize that I didn't look up C FAQs before.

    I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.
    Can you name a few which can help speed up my progress.

    thanks
     
    DanielJohnson, Feb 18, 2007
    #15
  16. DanielJohnson said:

    <snip>

    > I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.


    No, but it's "best of breed".

    > Can you name a few which can help speed up my progress.


    Clarity of thought is the best debugger there is. I've spent way too
    much time stepping uselessly through code, one line at a time. A penny
    of brainwork is worth a pound of code-stepping.

    But on the rare occasions when I do use a debugger, gdb is the one I
    use. On Linux, I see no more reason to use some other debugger instead
    of gdb than I do to use some other C compiler instead of gcc.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Feb 18, 2007
    #16
  17. DanielJohnson

    Guest

    On Feb 18, 4:25 pm, "DanielJohnson" <> wrote:
    > I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.
    > Can you name a few which can help speed up my progress.


    An afternoon spent learning gdb will "speed up your progress" day in,
    day out for the rest of your life.

    --
    "One of the great strengths of C from its earliest days has been its
    ability to manipulate sequences of characters."
    - P. J. Plauger
     
    , Feb 18, 2007
    #17
  18. "DanielJohnson" <> writes:
    [...]
    > I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.
    > Can you name a few which can help speed up my progress.


    <OT>
    "ddd" is a graphical frontend to gdb; it can also be used with other
    debuggers.
    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 18, 2007
    #18
  19. >>>>> "BP" == Bill Pursell <> writes:

    BP> It is a good idea, however, to make an effort to ensure that
    BP> your replies are correct. A very good way to gauge that you
    BP> understand something is to post an explanation here and see if
    BP> it gets trounced.

    Posting incorrect information here is not "an effort to ensure that
    your replies are correct," however. That involves going to the FAQ
    and to the good C references (K&R, the Standard, and Harbison &
    Steele) to ensure that you're correct *before* you post.

    That said, it's been nearly 20 years since I started programming in C,
    and I'm still really gratified when I post code here that nobody finds
    flaw in....

    Charlton



    --
    Charlton Wilbur
     
    Charlton Wilbur, Feb 18, 2007
    #19
  20. "Richard Heathfield" <> wrote in message
    > DanielJohnson said:
    >
    > <snip>
    >
    >> I am using gcc compiler on Ubuntu. Is gdb the only debugger in linux.

    >
    > No, but it's "best of breed".
    >
    >> Can you name a few which can help speed up my progress.

    >
    > Clarity of thought is the best debugger there is. I've spent way too
    > much time stepping uselessly through code, one line at a time. A penny
    > of brainwork is worth a pound of code-stepping.
    >
    > But on the rare occasions when I do use a debugger, gdb is the one I
    > use. On Linux, I see no more reason to use some other debugger instead
    > of gdb than I do to use some other C compiler instead of gcc.
    >

    My problem is that I am always switching systems.
    Once you get into a compile / debug habit, it is a real nuisance to have to
    adapt to a different debugger. So generally I just don't use them.
     
    Malcolm McLean, Feb 18, 2007
    #20
    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. Pieter Droogendijk

    segmentation fault (core dumped)

    Pieter Droogendijk, Dec 20, 2003, in forum: C Programming
    Replies:
    13
    Views:
    4,571
    no_name
    Jan 5, 2004
  2. Glen Hendry
    Replies:
    2
    Views:
    298
    James Willmore
    Oct 9, 2003
  3. ekilada

    Segmentation fault (core dumped)

    ekilada, Aug 8, 2006, in forum: Perl Misc
    Replies:
    9
    Views:
    707
    Ben Morrow
    Aug 13, 2006
  4. Replies:
    4
    Views:
    1,987
  5. Replies:
    2
    Views:
    41
    Akira Li
    Jun 1, 2014
Loading...

Share This Page