Jump to content

  • Log In with Google      Sign In   
  • Create Account

Code::Blocks "warning: deprecated conversion from string constant to ‘char*’"


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Acharis   Crossbones+   -  Reputation: 4003

Like
0Likes
Like

Posted 25 November 2012 - 06:00 PM

I'm using Code::Blocks and I'm getting this warning message "warning: deprecated conversion from string constant to ‘char*’". How can I disable it?

I googled and it's present in GCC 4.2.3+ and can be disabled by one of these:
-Wno-deprecated
-Wno-write-string

The thing is, I can't find any such setting in Code::Block's IDE :) Any clues how to disable it (globally, I would really hate to set it up for each project separately)?

Edited by Acharis, 25 November 2012 - 06:22 PM.

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9675

Like
6Likes
Like

Posted 25 November 2012 - 06:09 PM

That's not one of the warning's I'd consider useless. Pointing a non-const pointer at const memory is bad juju. Why not just use a const char * instead?

#3 Acharis   Crossbones+   -  Reputation: 4003

Like
-6Likes
Like

Posted 25 November 2012 - 06:21 PM

That's not one of the warning's I'd consider useless. Pointing a non-const pointer at const memory is bad juju. Why not just use a const char * instead?

Please, no religion :) Half of the people will say one should rewrite everything to const char * the other half will say it's an insane idea and this topic will turn into a discussion about good coding practices.

I just want to know how to disable it :)

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#4 Bacterius   Crossbones+   -  Reputation: 9304

Like
3Likes
Like

Posted 25 November 2012 - 06:32 PM


That's not one of the warning's I'd consider useless. Pointing a non-const pointer at const memory is bad juju. Why not just use a const char * instead?

Please, no religion Posted Image Half of the people will say one should rewrite everything to const char * the other half will say it's an insane idea and this topic will turn into a discussion about good coding practices.

I just want to know how to disable it Posted Image

const exists for a reason, if you end up in a situation where your const char* (which points to a string constant) has to be modified, your design has failed and const helps you catch that with -fpermissive. You are just asking for trouble here.

But if you need to disable it, go into your project options, "Other options" and write your extra compiler flags there. I'm not sure there's a way to set it globally but unless you have hundreds of projects...

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 wack   Members   -  Reputation: 1350

Like
2Likes
Like

Posted 25 November 2012 - 06:36 PM


That's not one of the warning's I'd consider useless. Pointing a non-const pointer at const memory is bad juju. Why not just use a const char * instead?

Please, no religion Posted Image Half of the people will say one should rewrite everything to const char * the other half will say it's an insane idea and this topic will turn into a discussion about good coding practices.

I just want to know how to disable it Posted Image


It's not really religion... It's just plain wrong. If you attempt to write to your char* that is actually a const char*, you might in fact crash your program.

Anyways... If you're sure what you are doing I will offer this little tip, although I haven't used Code::Blocks much.

There most certainly should be some kind of project option where you can specify custom command line arguments for the compiler. If you can't find any place in Code::Blocks to specify a custom command line, you could try adding it to your CXXFLAGS environment variable.

#6 Acharis   Crossbones+   -  Reputation: 4003

Like
-2Likes
Like

Posted 25 November 2012 - 06:52 PM

Sigh... OK, the thing is that all functions like this:

void message(char * s) {}

message("hello world");

would give a warning. It was never like that in the past, over the decades. Making this "Obsolete" after, well, so many generation when it was all right just make no sense :) As far as I know only newest GCC compiler has this flaw, no other compiler have it set that strict. It basicly makes above 99% of examples/code that was written in the past causing that kind of warning.

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#7 Bacterius   Crossbones+   -  Reputation: 9304

Like
1Likes
Like

Posted 25 November 2012 - 07:00 PM

Sigh... OK, the thing is that all functions like this:

	void message(char * s) {}
	
	message("hello world");
	
would give a warning. It was never like that in the past, over the decades. Making this "Obsolete" after, well, so many generation when it was all right just make no sense Posted Image As far as I know only newest GCC compiler has this flaw, no other compiler have it set that strict. It basicly makes above 99% of examples/code that was written in the past causing that kind of warning.

I don't understand what is wrong with adding const to the function signature?! Clearly the string must be read-only since you're passing a literal, so marking it with const is the right thing to do to ensure it can never be modified. Don't throw C++ type safety away just because you don't like reading warnings, you'll be glad to have that warning when your code explodes in your face.

Your code could work without error if you are rigorous enough to a) not modify the string b) not call any function that may modify the string, but it doesn't make OK to play with fire like that. Using const offers you a guarantee that the char pointer will never be written to by well-behaved functions and methods - what's wrong with that?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 JTippetts   Moderators   -  Reputation: 8662

Like
1Likes
Like

Posted 25 November 2012 - 07:11 PM

I use tolua++ to generate Lua bindings and unfortunately, as it currently stands, the generated binding code is filled to the brim with these warnings. They are harmless in the context of what tolua++ generates (ie, the code doesn't do anything "bad"), and it's even possible that a newer version of tolua++ has eliminated them, but for the time being I just use compiler flag.

You can do it per-project as Bacterius suggested, or you can do it globally. Go to Settings->Global Compiler Settings->Other options and add the -Wno-write-strings there. I actually don't recommend you do it globally, though, since you shouldn't get in the habit of ignoring warnings.

#9 Acharis   Crossbones+   -  Reputation: 4003

Like
0Likes
Like

Posted 25 November 2012 - 08:09 PM

You can do it per-project as Bacterius suggested, or you can do it globally. Go to Settings->Global Compiler Settings->Other options and add the -Wno-write-strings there. I actually don't recommend you do it globally, though, since you shouldn't get in the habit of ignoring warnings.

Thanks, that solved it.

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#10 Trienco   Crossbones+   -  Reputation: 2224

Like
2Likes
Like

Posted 25 November 2012 - 11:24 PM

I use tolua++ to generate Lua bindings and unfortunately, as it currently stands, the generated binding code is filled to the brim with these warnings.


Maybe I'm confused by the name, but ++ suggests that it creates bindings for C++. Everything producing "char*" instead of "const char*" (unless the function might actually modify the string) should be considered flat out broken and worthy of a bug report. Mostly because I'm extremely sick of having to add hacks and workarounds when using C++ strings and c_str. Casting away the const feels like slapping a time bomb and copying the string to a vector<char> is just irritating.
f@dzhttp://festini.device-zero.de




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS