?

Log in

No account? Create an account

Previous Entry | Next Entry

The Department of Redundancy Department

At work today, I was asked to patch some code on a project that I haven't worked on for years, and even then I only worked on the TCL side. The fix for today required me to delve into the C++ side of the code, though, to properly pass some data around. While working on this unfamiliar code, I ran across this stunning little snippet: (paraphrased)

const_cast<char *>(string("true").c_str());

...

...

For those not familiar with C++, you can write that same line exactly as:

(char*)"true"

Writing it the first way is like taking a glass of water, putting it in the freezer, then thawing it in the microwave to get a glass of water.

Obviously the last person to work on this code wasn't very C++ savvy, to say the least.

This isn't even covering the fact that it's kind of dodgy to cast a read-only string as writable, but at least there are semi-valid reasons to do that.

Comments

( 1 comment — Leave a comment )
credendovides
Apr. 3rd, 2007 05:31 am (UTC)
A properly compiled bit of code would put the string in a write-only page so any attempt to write to it would cause a crash. It would be a fun one to watch someone try and track down until they check what that address it crashed on is actually pointing to.

And the first bit, I knew what it did, but your water analogy was rather amusing.
( 1 comment — Leave a comment )