API : constness ?

Erik Max Francis max at alcyone.com
Tue Jun 1 12:29:11 EDT 2004


Isaac To wrote:

> This is not quite right.  A string literal is always put into
> read-only
> memory, no matter whether you specify const or not.

That's not true.  It _can_ be put in read-only memory.  It is not
mandated to.

> So:
> 
>   int main() {
>       char *str = "abc";
>       str[0] = '\b'; // segmentation fault
>   }

No, this is undefined behavior.  It's undefined behavior so that the
implementation _can_ put the string literal in read-only memory, but the
implementation is under no obligation to.

-- 
 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Chastity the most unnatural of the sexual perversions.
    -- Aldous Huxley



More information about the Python-list mailing list