Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Christian Gollwitzer Newsgroups: comp.lang.python,comp.lang.c++ Subject: Re: Question about math.pi is mutable Followup-To: comp.lang.c++ Date: Sun, 8 Nov 2015 09:00:27 +0100 Organization: A noiseless patient Spider Lines: 66 Message-ID: References: <87d1vlzy4p.fsf@elektro.pacujo.net> <878u69zxww.fsf@elektro.pacujo.net> <87y4e9y9j6.fsf@elektro.pacujo.net> <87d1vlx7bg.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 8 Nov 2015 07:58:12 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="09360e0ab07672f4bcf79b9f96c5414e"; logging-data="6336"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/To8SZwPOJAYm5sjUFwnJ7zgkmkdrdKWQ=" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <87d1vlx7bg.fsf@elektro.pacujo.net> Cancel-Lock: sha1:p7xBCj5upUCMqcsRpkXR0iD1vMA= Xref: csiph.com comp.lang.python:98426 comp.lang.c++:38661 Am 08.11.15 um 08:45 schrieb Marko Rauhamaa: > Grant Edwards : > >> On 2015-11-07, Marko Rauhamaa wrote: >>> "const" is a very ineffective tool that clutters the code and forces >>> you to sprinkle type casts around your code. >> >> But it allows the compiler to warn you if you pass a pointer to a >> read-only data to a function that expects a pointer to writable data. > > Unfortunately, it doesn't: > > ======================================================================== > #include > #include > > int main() > { > const char name[] = "Tom"; > char *p = strstr(name, "Tom"); > strcpy(p, "Bob"); > printf("name = %s\n", name); > return 0; > } > ======================================================================== > > $ cc -o prog prog.c > $ ./prog > Bob > > No warning. > That is strange. In C, I can see thet problem here, because it is impossible to define a const correct strstr. But in C++ I have expected that according to the overload, the const version of strstr would be selected (http://www.cplusplus.com/reference/cstring/strstr/ ) . Still: apfelkiste:Tests chris$ cat prog.cpp #include #include int main() { const char name[] = "Tom"; char *p = strstr(name, "Tom"); // this line should be an error strcpy(p, "Bob"); printf("name = %s\n", name); return 0; } apfelkiste:Tests chris$ g++ -Wall prog.cpp apfelkiste:Tests chris$ ./a.out Bus error: 10 It segfaults because on OSX, const can be stored in write-only memory. apfelkiste:Tests chris$ g++ --version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin12.6.0 Thread model: posix Christian