strcat() and segmentation faults. But malloc() already done.

Discussion in 'C Programming' started by Stanley S, Dec 20, 2005.

  1. Stanley S

    Stanley S Guest

    Hi,

    I'm puzzled. Why does the following cause a seg fault? Notwithstanding
    that I've already malloc() a certain space for "Hello".

    I do understand that using a fixed length array will work very well.
    But I wish to find out how can this be achieve using pointers.

    Thank you.

    Stan

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

    int main(int argc, char *argv[])
    {

    char *Hello = NULL;
    char *World = NULL;
    Hello = malloc(100);
    Hello = "hello";
    World = "world";
    strcat(Hello, World);
    printf("%s\n",Hello);

    return 0;
    }
     
    Stanley S, Dec 20, 2005
    #1
    1. Advertising

  2. Stanley S

    Artie Gold Guest

    Stanley S wrote:
    > Hi,
    >
    > I'm puzzled. Why does the following cause a seg fault? Notwithstanding
    > that I've already malloc() a certain space for "Hello".
    >
    > I do understand that using a fixed length array will work very well.
    > But I wish to find out how can this be achieve using pointers.
    >
    > Thank you.
    >
    > Stan
    >
    > #include <stdio.h>
    > #include <stdlib.h>

    #include <string.h> /* for the `str*' functions you call below */
    >
    > int main(int argc, char *argv[])
    > {
    >
    > char *Hello = NULL;
    > char *World = NULL;
    > Hello = malloc(100);

    Here you (most likely -- you really *should* error check) allocate 100
    chars of memory to the pointer `Hello'...
    > Hello = "hello";

    ....which you promptly throw away (causing a memory leak) when you assign
    the location of the literal string "hello" to it.

    What you wanted was:
    strcpy(Hello, "hello");
    > World = "world";
    > strcat(Hello, World);
    > printf("%s\n",Hello);
    >
    > return 0;
    > }
    >

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Dec 20, 2005
    #2
    1. Advertising

  3. Stanley S

    Eric Sosman Guest

    Stanley S wrote On 12/20/05 12:22,:
    > Hi,
    >
    > I'm puzzled. Why does the following cause a seg fault? Notwithstanding
    > that I've already malloc() a certain space for "Hello".
    >
    > I do understand that using a fixed length array will work very well.
    > But I wish to find out how can this be achieve using pointers.
    >
    > Thank you.
    >
    > Stan
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char *argv[])
    > {
    >
    > char *Hello = NULL;
    > char *World = NULL;
    > Hello = malloc(100);


    Now `Hello' points to an anonymous chunk of memory
    big enough to hold 100 characters. (Assuming malloc()
    succeeded, which you should have checked.)

    > Hello = "hello";


    Now `Hello' points to a completely different piece
    of memory, namely, a six-character array initialized
    with 'h','e','l','l','o','\0'. It no longer points to
    the memory obtained from malloc(). (As an aside, since
    you no longer have any record of where that memory is,
    you cannot find it even to free() it -- this is what's
    known as a "memory leak.")

    You probably wanted `strcpy(Hello, "hello");'.

    > World = "world";
    > strcat(Hello, World);


    This attempts to append five more characters to the
    six-character array, and there isn't room for them. The
    effects are in general unpredictable; someday you will
    come to appreciate how helpful a nice reproducible SIGSEGV
    can be. You won't always be so lucky.

    > printf("%s\n",Hello);
    >
    > return 0;
    > }
    >
     
    Eric Sosman, Dec 20, 2005
    #3
  4. Stanley S

    Stanley S Guest

    Thanks a million guys.
     
    Stanley S, Dec 20, 2005
    #4
  5. MTA - Google (was: Re: strcat() and segmentation faults. But malloc() already done.)

    Stanley S <> wrote:

    > Thanks a million guys.


    It is proper Usenet etiquette to include the relevant portions of the text
    you are replying to. To do this using Google groups, please follow the
    instructions below, penned by Keith Thompson:

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 20, 2005
    #5
  6. Stanley S a écrit :
    > Hi,
    >
    > I'm puzzled. Why does the following cause a seg fault? Notwithstanding
    > that I've already malloc() a certain space for "Hello".
    >
    > I do understand that using a fixed length array will work very well.
    > But I wish to find out how can this be achieve using pointers.
    >
    > Thank you.
    >
    > Stan
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char *argv[])
    > {
    >
    > char *Hello = NULL;
    > char *World = NULL;
    > Hello = malloc(100);


    The value you have given to Hello is the address of an allocated block.

    > Hello = "hello";


    Now, you are replacing this value by the address of a non mutable
    string. Sounds nuts, isn't it ?

    > World = "world";
    > strcat(Hello, World);


    Of course, the destination zone not being writable, the behaviour is
    undefined. Anything can happen.

    I suggest you consider a function to copy the string (say strcpy(), for
    instance...)

    > printf("%s\n",Hello);
    >
    > return 0;
    > }


    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Dec 20, 2005
    #6
  7. Stanley S wrote:
    > Hi,
    >
    > I'm puzzled. Why does the following cause a seg fault? Notwithstanding
    > that I've already malloc() a certain space for "Hello".


    And promptly lost it ...

    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char *argv[])
    > {
    >
    > char *Hello = NULL;
    > char *World = NULL;
    > Hello = malloc(100);
    > Hello = "hello";


    You just overwrote the pointer Hello to point to the string literal
    "hello". Not only have you now lost the pointer to the space allocated
    in the previous line, but the string literal "hello" is not guaranteed
    to be writable.

    > World = "world";
    > strcat(Hello, World);


    Even if the string literal "hello" were writable, it is only 6 chars
    long, so you are now trying to write into the non-allocated space beyond
    the probably unwritable string "hello".

    > printf("%s\n",Hello);
    >
    > return 0;
    > }
    >
     
    Martin Ambuhl, Dec 20, 2005
    #7
  8. Stanley S

    Mark B Guest

    Re: MTA - Google (was: Re: strcat() and segmentation faults. But malloc() already done.)

    "Christopher Benson-Manica" <> wrote in message
    news:do9heq$q1v$...
    > Stanley S <> wrote:
    >
    >> Thanks a million guys.

    >
    > It is proper Usenet etiquette to include the relevant portions of the text
    > you are replying to. To do this using Google groups, please follow the
    > instructions below, penned by Keith Thompson:

    <snip>
    > ... Flames welcome.


    You asked for it... ;-)

    <ot rant>
    Now you're just being an idiot...
    Problem was obviously solved and he was thanking those who answered.
    The 'relevant portions of the text' he was replying to are no longer
    relevant.
    Stop being a dumbass.

    Btw: Where in the usenet etiquette does it state that it is acceptable to
    repeatedly post instructions to people who don't follow what you deam to
    be acceptable usenet etiquette?

    </ot rant>
     
    Mark B, Dec 20, 2005
    #8
  9. Re: MTA - Google (was: Re: strcat() and segmentation faults. Butmalloc() already done.)

    "Mark B" <> writes:
    [...]
    > Btw: Where in the usenet etiquette does it state that it is acceptable to
    > repeatedly post instructions to people who don't follow what you deam to
    > be acceptable usenet etiquette?


    We don't (usually) repeatedly post instructions to the same people.
    We post instructions in response to people who probably haven't seen
    them before. If you did a search for the authors of the parent
    articles of all the followups containing Google instructions, I think
    you'd find most of them are unique, and many of them get the message
    and start to use Google properly.

    See also <http://cfaj.freeshell.org/google/>.

    --
    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, Dec 20, 2005
    #9
  10. Stanley S

    Flash Gordon Guest

    Re: MTA - Google

    Mark B wrote:
    > "Christopher Benson-Manica" <> wrote in message
    > news:do9heq$q1v$...
    >> Stanley S <> wrote:
    >>
    >>> Thanks a million guys.

    >> It is proper Usenet etiquette to include the relevant portions of the text
    >> you are replying to. To do this using Google groups, please follow the
    >> instructions below, penned by Keith Thompson:

    > <snip>
    >> ... Flames welcome.

    >
    > You asked for it... ;-)
    >
    > <ot rant>
    > Now you're just being an idiot...


    No, he is posting useful advice to those unfortunate enough to be using
    Google for whatever reason.

    > Problem was obviously solved and he was thanking those who answered.
    > The 'relevant portions of the text' he was replying to are no longer
    > relevant.


    Even then it is customary to at least leave a line or two in.

    > Stop being a dumbass.


    There is no need to be insulting even if you disagree.

    > Btw: Where in the usenet etiquette does it state that it is acceptable to
    > repeatedly post instructions to people who don't follow what you deam to
    > be acceptable usenet etiquette?


    It is not just what Christopher considers acceptable, it is normal
    usenet etiquette. It is generally considered acceptable here to point
    out normal posting conventions to those who are not aware of it, just as
    it is considered acceptable to point people at the FAQ when the question
    they ask is answered in the FAQ. You can tell it is generally considered
    acceptable because a significant portion of the regulars do it and the
    only regular poster I see complaining about it is a self acknowledged troll.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 20, 2005
    #10
  11. Stanley S

    Mark B Guest

    Re: MTA - Google

    "Flash Gordon" <> wrote in message
    news:-gordon.me.uk...
    > Mark B wrote:
    >> "Christopher Benson-Manica" <> wrote in
    >> message news:do9heq$q1v$...
    >>> Stanley S <> wrote:


    <snip>
    >>> ... Flames welcome.

    >>
    >> You asked for it... ;-)


    Tell me the stick isn't lodged that far up your ass...
    you do realize my post was made in good humor, no?

    <snip>
    > There is no need to be insulting even if you disagree.


    Is it possible to 'flame' someone without being insulting?

    > You can tell it is generally considered acceptable because a significant
    > portion of the regulars do it


    Yes, I know... over and over and over again ;-)

    > and the only regular poster I see complaining about it is a self
    > acknowledged troll.


    I've been posting (on and off) since 1999, albeit my email
    address has changed a few times (and may need to be
    changed yet again as sober@localbar is no longer valid.)

    Granted I don't post daily, or even weekly if I don't have
    anything to contribute (normally by the time I check OP
    questions have already been answered...) but I still
    consider myself a clc regular/poster.

    I'd wager the average clc regular is tired of seeing the
    'learn to quote in google' messages and would rather
    that the 'proper posting enforcers' did like we do
    (particularly with a msg such as the 'thank you' that
    started this rant)... move on to the next message.

    The reason you don't see more complaints is because
    we know that it will only result in more crap that we
    don't want to read.

    That being said, let me be first to wish everyone a
    Merry Christmas and Happy New Year (in case I
    don't post again between now and then - plan on
    taking a few days off and don't typically check clc
    when I'm not working ;-)

    Be safe and enjoy the holidays.
    Mark
     
    Mark B, Dec 20, 2005
    #11
  12. Re: MTA - Google

    Mark B <> wrote:

    > I'd wager the average clc regular is tired of seeing the
    > 'learn to quote in google' messages and would rather
    > that the 'proper posting enforcers' did like we do
    > (particularly with a msg such as the 'thank you' that
    > started this rant)... move on to the next message.


    You'll notice that the vast majority of these messages (at least those
    now coming from me, and from Keith and others) are identifiable from
    their subject line. If you are using a reasonable newsreader,
    filtering posts by subject is a trivial matter.

    > The reason you don't see more complaints is because
    > we know that it will only result in more crap that we
    > don't want to read.


    It may also be that those who don't want to read it are in fact not
    doing so. I sincerely hope you will see this post after your holiday,
    since you seem to be suffering needlessly from a malady easily cured
    by judicious use of newsreading software. Do enjoy yourself :)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 20, 2005
    #12
  13. Stanley S

    Flash Gordon Guest

    Re: MTA - Google

    Mark B wrote:
    > "Flash Gordon" <> wrote in message
    > news:-gordon.me.uk...
    >> Mark B wrote:
    >>> "Christopher Benson-Manica" <> wrote in
    >>> message news:do9heq$q1v$...
    >>>> Stanley S <> wrote:

    >
    > <snip>
    >>>> ... Flames welcome.
    >>> You asked for it... ;-)

    >
    > Tell me the stick isn't lodged that far up your ass...


    There isn't one, there is also no need for that language. It may well be
    intended as humorous, but that is not the way it reads to me. It is
    certainly something more often said in my experience when intending to
    be insulting than when trying to be humorous.

    Before you ask, yes I do have a sense of humour, it is just aligned
    differently to yours.

    > you do realize my post was made in good humor, no?


    It was not obvious that anything beyond the first line was intended as
    humours.

    > <snip>
    >> There is no need to be insulting even if you disagree.

    >
    > Is it possible to 'flame' someone without being insulting?


    Yes.

    >> You can tell it is generally considered acceptable because a significant
    >> portion of the regulars do it

    >
    > Yes, I know... over and over and over again ;-)
    >
    >> and the only regular poster I see complaining about it is a self
    >> acknowledged troll.

    >
    > I've been posting (on and off) since 1999, albeit my email
    > address has changed a few times (and may need to be
    > changed yet again as sober@localbar is no longer valid.)


    I actually checked on your name, not email address, and it was not
    obvious to me that you have been around for a long time.

    > Granted I don't post daily, or even weekly if I don't have
    > anything to contribute (normally by the time I check OP
    > questions have already been answered...) but I still
    > consider myself a clc regular/poster.


    Doesn't bother me either way. That would still only bring the total up
    to two, which is fewer than the number of regulars posting such comments
    or supporting them when the posts are challenged.

    > I'd wager the average clc regular is tired of seeing the
    > 'learn to quote in google' messages and would rather
    > that the 'proper posting enforcers' did like we do
    > (particularly with a msg such as the 'thank you' that
    > started this rant)... move on to the next message.


    Well, you are certainly entitled to your opinion. However, the opinion
    of some of us is that if the problem is not pointed out to people the
    group will quickly become unusable to a number of us. I certainly would
    not stay if the majority of the posts were context less or top posting.

    > The reason you don't see more complaints is because
    > we know that it will only result in more crap that we
    > don't want to read.


    Entirely possible, after all it is generating more posts ;-)

    > That being said, let me be first to wish everyone a
    > Merry Christmas and Happy New Year (in case I
    > don't post again between now and then - plan on
    > taking a few days off and don't typically check clc
    > when I'm not working ;-)
    >
    > Be safe and enjoy the holidays.


    Thanks.

    Enjoy your holiday and anything you celebrate around this time.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 20, 2005
    #13
  14. Re: MTA - Google

    Christopher Benson-Manica <> writes:
    > Mark B <> wrote:
    >> I'd wager the average clc regular is tired of seeing the
    >> 'learn to quote in google' messages and would rather
    >> that the 'proper posting enforcers' did like we do
    >> (particularly with a msg such as the 'thank you' that
    >> started this rant)... move on to the next message.

    >
    > You'll notice that the vast majority of these messages (at least those
    > now coming from me, and from Keith and others) are identifiable from
    > their subject line. If you are using a reasonable newsreader,
    > filtering posts by subject is a trivial matter.


    Actually, I don't change the subject header when I post a followup.
    On the other hand, I've shortened by Google advice to a single URL,
    and I often combine it with a substantive response.

    --
    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, Dec 21, 2005
    #14
  15. Re: MTA - Google

    Keith Thompson <> wrote:

    > Actually, I don't change the subject header when I post a followup.
    > On the other hand, I've shortened by Google advice to a single URL,
    > and I often combine it with a substantive response.


    My apologies for misrecollecting.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 21, 2005
    #15
  16. Re: MTA - Google

    "Mark B" <> writes:
    > "Flash Gordon" <> wrote:
    >> and the only regular poster I see complaining about it is a self
    >> acknowledged troll.


    > I've been posting (on and off) since 1999,


    There, there. No need to apologise - everybody was a newbie once.

    > albeit my email address has changed a few times (and may need to be
    > changed yet again as sober@localbar is no longer valid.)


    Perhaps you need to investigate a more stable mail host. You could
    join the ACM.

    > I'd wager the average clc regular is tired of seeing the 'learn to
    > quote in google' messages and would rather that the 'proper posting
    > enforcers' did like we do (particularly with a msg such as the
    > 'thank you' that started this rant)... move on to the next message.


    I may not be average, but I'd rather that the myriad people at whom
    the 'learn to quote in google' messages are aimed actually paid
    attention to them BEFORE the next time they posted.

    It's a faint hope.

    mlp
     
    Mark L Pappin, Dec 22, 2005
    #16
  17. Re: MTA - Google

    Mark L Pappin <> writes:
    > "Mark B" <> writes:
    >> "Flash Gordon" <> wrote:

    [...]
    >> I'd wager the average clc regular is tired of seeing the 'learn to
    >> quote in google' messages and would rather that the 'proper posting
    >> enforcers' did like we do (particularly with a msg such as the
    >> 'thank you' that started this rant)... move on to the next message.

    >
    > I may not be average, but I'd rather that the myriad people at whom
    > the 'learn to quote in google' messages are aimed actually paid
    > attention to them BEFORE the next time they posted.
    >
    > It's a faint hope.


    A more realistic hope is that Google will listen to us and fix their
    broken interface.

    If you haven't already, please visit
    <http://groups-beta.google.com/support/bin/request.py?contact_type=features>
    and vote for "Default quoting of previous message in replies".

    --
    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, Dec 22, 2005
    #17
    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. Mathias

    Segmentation faults using threads

    Mathias, Feb 13, 2007, in forum: Python
    Replies:
    8
    Views:
    321
    John Nagle
    Feb 14, 2007
  2. Digital Puer
    Replies:
    18
    Views:
    744
    Ron Natalie
    Dec 28, 2005
  3. ZillionDollarSadist

    Segmentation faults on "new"

    ZillionDollarSadist, Jan 17, 2007, in forum: C++
    Replies:
    6
    Views:
    367
    Jacek Dziedzic
    Jan 18, 2007
  4. Stephen Anderson
    Replies:
    6
    Views:
    178
    Stephen Anderson
    May 8, 2008
  5. Bob Florian
    Replies:
    3
    Views:
    93
    Phillip Gawlowski
    May 21, 2008
Loading...

Share This Page