On 26/05/2017 12:46, Steve D'Aprano wrote:
On Thu, 25 May 2017 11:26 am, Chris Angelico wrote:

And why should they try to stop you? The whole point of undefined
behaviour is that you shouldn't be doing this, so if you do, the
interpreter's allowed to do anything.

Does the C specification actually refer to this as undefined? I'm not a C
expert, but it seems to me that it is defined as an error. I tried
compiling that code:


[steve@ando c]$ cat stringassign.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  "abcdef"[3] = 'z';
  puts("abcdef");
  return 0;
}
[steve@ando c]$ gcc stringassign.c
stringassign.c: In function ‘main’:
stringassign.c:6: error: assignment of read-only location


but I couldn't get it to succeed.

Compilers will vary.

In gcc for Windows, I get the same message but as a warning.

With DMC, nothing. With Pelles C, nothing. With lccwin, nothing. With Tiny C, nothing. With MSVC 2008, nothing.

(Only my own compiler will give a hard error, but that's because I specify detect that. In general, the language can't stop you writing to such a string, one way or another.)

Note that compiled as C++, I think this has to fail (because string literals have slightly different types compared with C).

--
bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to