Re: segmentation fault

Discussion in 'C++' started by Ron Natalie, Jul 14, 2003.

  1. Ron Natalie

    Ron Natalie Guest

    "Ramin" <> wrote in message news:...

    > char *string2 = "Another string";

    \
    > Why the following statement gives segmentation fault?!?!
    > \*********************************************************/
    > convert( string2 ); // Segmentation fault!

    \
    For string2, string literals not used to initialize character arrays are statically allocated at some
    unspecified location. They MUST be treated as an array of const char despite the
    fact that the lanuguage allows a deprecated conversion to (non-const) char*. Any
    attempt to actually change the characters is undefined behavior. In your case, your
    implmenetation almost certainly places the array in read-only storage, hence the
    fault.

    The string literal for string1 is just an initializer for the the array. string1 is an array of
    (count the chars in the literal, add one for null) char that's allocated as a local auto variable.
    You haven't made this const, so you can modify it to your hearts content.
    Ron Natalie, Jul 14, 2003
    #1
    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:
    843
  2. Pud
    Replies:
    0
    Views:
    558
  3. Replies:
    0
    Views:
    510
  4. Ivan Vecerina
    Replies:
    0
    Views:
    471
    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,564
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page