Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Question about math.pi is mutable

28 views
Skip to first unread message

Christian Gollwitzer

unread,
Nov 8, 2015, 3:00:42 AM11/8/15
to
Am 08.11.15 um 08:45 schrieb Marko Rauhamaa:
> Grant Edwards <inv...@invalid.invalid>:
>
>> On 2015-11-07, Marko Rauhamaa <ma...@pacujo.net> 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 <stdio.h>
> #include <string.h>
>
> 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 <cstdio>
#include <cstring>

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

Paavo Helde

unread,
Nov 8, 2015, 3:54:39 AM11/8/15
to
Christian Gollwitzer <auri...@gmx.de> wrote in news:n1mvak$660$1@dont-
email.me:
strstr() and strcpy() are not guaranteed to be present when you include
<cstring>, one should use std::strstr() and std::strcpy() instead. But
this is probably not important here.

> 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.

FWIW, MSVC 2013 produces an error:

1> main.cpp
1>main.cpp(6): error C2440: 'initializing' : cannot convert from 'const
char *' to 'char *'
1> Conversion loses qualifiers
1>main.cpp(7): error C4996: 'strcpy': This function or variable may be
unsafe. Consider using strcpy_s instead. To disable deprecation, use
_CRT_SECURE_NO_WARNINGS. See online help for details.
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC
\include\string.h(112) : see declaration of 'strcpy'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

Ian Collins

unread,
Nov 8, 2015, 4:03:48 AM11/8/15
to
You must be using an old compiler:

# gcc4.9/bin/g++ x.cc
x.cc: In function ‘int main()’:
x.cc:7:31: error: invalid conversion from ‘const char*’ to ‘char*’
[-fpermissive]
char *p = strstr(name, "Tom"); // this line should be an error

--
Ian Collins
0 new messages