Re: whether this is valid

From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 06/26/04


Date: 26 Jun 2004 21:01:16 GMT

madhur <madhur@sdf.com> scribbled the following:
> Hello

> char *a="str";
> strcpy(a,"abc");

> I have learnt that since "a" is a string literal, it might be allocated on
> read only memory on some implementations.
> Is it correct to modify a string literal using this way.

Not, it's not correct. It might work or "work" on some implementations
but that's due to either special allowances or sheer dumb luck. The
above code causes undefined behaviour and thus should never be relied
on.

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"As we all know, the hardware for the PC is great, but the software sucks."
   - Petro Tyschtschenko


Relevant Pages

  • Re: whether this is valid
    ... In 'comp.lang.c', "madhur" wrote: ... > read only memory on some implementations. ... > Is it correct to modify a string literal using this way. ...
    (comp.lang.c)
  • Re: whether this is valid
    ... madhur wrote: ... > allocated on read only memory on some implementations. ... > Is it correct to modify a string literal using this way. ...
    (comp.lang.c)
  • Re: whether this is valid
    ... > read only memory on some implementations. ... > Is it correct to modify a string literal using this way. ... If it were legal, why on earth would you do it? ...
    (comp.lang.c)
  • Re: whether this is valid
    ... >read only memory on some implementations. ... >Is it correct to modify a string literal using this way. ... to declare it const char *a so the compiler can detect if you try to ...
    (comp.lang.c)
  • whether this is valid
    ... I have learnt that since "a" is a string literal, ... read only memory on some implementations. ... Is it correct to modify a string literal using this way. ...
    (comp.lang.c)