I completely agree with the other answers, I just want to add that g ++ (at least version 4.4) really catches these obsolete conversions as warnings at any warning level (if previous versions don't do this by default, maybe you need to raise the level warnings):
#include <iostream> using namespace std; void WithConst(const char * Str) { cout<<Str<<endl; } void WithoutConst_NoEdit(char * Str) { cout<<Str<<endl; } void WithoutConst_Edit(char * Str) { *Str='a'; cout<<Str<<endl; } int main() { WithConst("Test"); WithoutConst_NoEdit("Test"); WithoutConst_Edit("Test"); return 0; }
matteo@teoubuntu :~/cpp/test$ g++ --version g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. matteo@teoubuntu :~/cpp/test$ g++ -O3 lit_const_corr.cpp -o lit_const_corr.x lit_const_corr.cpp: In function 'int main()': lit_const_corr.cpp:24: warning: deprecated conversion from string constant to 'char*' lit_const_corr.cpp:25: warning: deprecated conversion from string constant to 'char*' matteo@teoubuntu :~/cpp/test$ g++ -O3 -Wall lit_const_corr.cpp -o lit_const_corr.x lit_const_corr.cpp: In function 'int main()': lit_const_corr.cpp:24: warning: deprecated conversion from string constant to 'char*' lit_const_corr.cpp:25: warning: deprecated conversion from string constant to 'char*' matteo@teoubuntu :~/cpp/test$ g++ -O3 -Wall -Wextra -ansi -pedantic lit_const_corr.cpp -o lit_const_corr.x lit_const_corr.cpp: In function 'int main()': lit_const_corr.cpp:24: warning: deprecated conversion from string constant to 'char*' lit_const_corr.cpp:25: warning: deprecated conversion from string constant to 'char*'
In addition, there is something interesting that happens under the hood: if I compile it without optimization, it โjust does what the code saysโ, so it crashes because it tries to write to a memory only for reading:
matteo@teoubuntu :~/cpp/test$ g++ -Wall -Wextra -ansi -pedantic lit_const_corr.cpp -o lit_const_corr.x lit_const_corr.cpp: In function 'int main()': lit_const_corr.cpp:24: warning: deprecated conversion from string constant to 'char*' lit_const_corr.cpp:25: warning: deprecated conversion from string constant to 'char*' matteo@teoubuntu :~/cpp/test$ ./lit_const_corr.x Test Test Segmentation fault
but , if you turn on the optimizer, there is no failure:
matteo@teoubuntu :~/cpp/test$ g++ -O3 -Wall -Wextra -ansi -pedantic lit_const_corr.cpp -o lit_const_corr.x lit_const_corr.cpp: In function 'int main()': lit_const_corr.cpp:24: warning: deprecated conversion from string constant to 'char*' lit_const_corr.cpp:25: warning: deprecated conversion from string constant to 'char*' matteo@teoubuntu :~/cpp/test$ ./lit_const_corr.x Test Test Test
I believe this is due to some kind of magic optimization trick, but I donโt understand why it is applied; any idea?
Adding
When I declare char * foo = "bar", it actually complains. But when I declare char foo [] = "bar", it does not
Hey, be careful not to confuse two things: with
char * foo = "bar";
you declare a pointer to char, and you assign it the address of the literal โbarโ, which is actually stored in some read-only memory location (usually this is the part of the executable that is mapped to Memory). Instead with
char foo[]="bar";
you declare and allocate RW-memory (on the stack or somewhere else, depending on the context) for an array of characters that is initialized with the value "bar", but it is not associated with the string table at all, and it is completely natural to change this string.