void do_something( CHAR_DATA *ch, char *argument )
putenv( "TERM=vt100" );
putenv( "COLUMNS=80" );
putenv( "LINES=24" );
char dummy[] = "Hello";
char *a = dummy;
const char *b = dummy; //Points to a const char
char * const c = dummy; //A const pointer to a char
const char *const d = dummy; //A const pointer to a const char
*a = 'A'; //Legal
*b = 'B'; //Illegal, b points of a const char
*c = 'C'; //Legal, c is a const pointer to a char, so it can modify the contents
*d = 'D'; //Illegal, d points of a const char
b = b+1; //Legal, b points to a const char, but you can reseat the pointer
c = c+1; //Illegal, c is a const pointer, so it cannot be changed
vector < string > vector_argument( string arg, int chop )
{
vector < string > v;
char cEnd;
int passes = 0;
if( arg.find( '\'' ) < arg.find( '"' ) )
cEnd = '\'';
else
cEnd = '"';
while( !arg.empty( ) )
{
string::size_type space = arg.find( ' ' );
if( space == string::npos )
space = arg.length( );
string::size_type quote = arg.find( cEnd );
if( quote != string::npos && quote < space )
{
arg = arg.substr( quote + 1, arg.length( ) );
if( ( quote = arg.find( cEnd ) ) != string::npos )
space = quote;
else
space = arg.length( );
}
string piece = arg.substr( 0, space );
strip_lspace( piece );
if( !piece.empty( ) )
v.push_back( piece );
if( space < arg.length( ) - 1 )
arg = arg.substr( space + 1, arg.length( ) );
else
break;
/*
* A -1 indicates you want to proceed until arg is exhausted
*/
if( ++passes == chop && chop != -1 )
{
strip_lspace( piece );
v.push_back( arg );
break;
}
}
return v;
}
When compiling C, give string constants the type `const
char[LENGTH]' so that copying the address of one into a
non-`const' `char *' pointer will get a warning; when compiling
C++, warn about the deprecated conversion from string constants to
`char *'. These warnings will help you find at compile time code
that can try to write into a string constant, but only if you have
been very careful about using `const' in declarations and
prototypes. Otherwise, it will just be a nuisance; this is why we
did not make `-Wall' request these warnings.
The -Wwrite-strings warning flag is an optional extra flag in GCC <= 4.1x but not in GCC >= 4.2. If you don't have Gc 4.2, you can get a face full of the following just for the fun of it. I did this on SmaugFUSS, choose your own favorite codebase to have fun with.
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
act_comm.c:2784: warning: deprecated conversion from string constant to 'char*''
I have been unable to find a means of getting the compiler to turn this off with a "stop spamming me" style option. So it would appear that upgrading to GCC 4.2 is going to give everyone here an assload of grief. For what appears to be very little gain.
So why is this such a problem? Isn't this supposed to be good? You might think so until you realize that every codebase that has a command format like this:
is going to complain bitterly that argument isn't a const. Yes. I can see the light bulbs coming on now. What of commands that break that up with one_argument? What about commands that trigger a help response by calling do_something( ch, "" );
They'll generate the same crapload of warnings as above.
Ordinarily I'm all in favor of GNU tightening things up and producing warnings and errors on legitimate problems, but I think springing this on people without mentioning it in the release notes is just wrong. A quick Googling reveals the specific error only has a couple hundred hits, but some of them were generated on large high profile projects like KDE. Now presumably KDE is run by professional coders. Usually well above the caliber you find on the average MUD. If *THEY* have thousands of lines of code with this problem, is it entirely unreasonable that our hobby projects might have the same problems? How long do you think the KDE guys are gonna need to clean up that gigantic mess? How long is it going to take MUD coders to clean up this mess? What about all those old dinosaur codebases that nobody has updated in 15 years? This is only going to add more crap to sift through to look for legitimate errors.