MESSAGE
DATE | 2010-03-03 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics - agregate data
|
On Wed, Mar 03, 2010 at 11:31:23PM -0500, Ruben Safir wrote: > > string_ex.cc > > > > -------------------------------------------------- > > > > #include > > #include "test.h" > > using namespace std; > > > > void stingexample(){ > > > > char test[] = "My Dog has Fleas\n"; > > const char * test2 = "My Dog has Fleas\n"; > > cout << test; > > test[3] = 'C'; > > test[4] = 'a'; > > test[5] = 't'; > > cout << test; > > cout << test2; > > const_cast(test2[3] = 'C'); > > const_cast(test2[4] = 'a'); > > const_cast(test2[5] = 't'); > > cout << test2; > > > > } > > I'd like to make a correction here as follows > > void stingexample(){ > > char test[] = "My Dog has Fleas\n"; > const char * test2 = "My Dog has Fleas\n"; > cout << test; > test[3] = 'C'; > test[4] = 'a'; > test[5] = 't'; > cout << test; > cout << test2; > const_cast(test2[3]) = 'C'; > const_cast(test2[4]) = 'a'; > const_cast(test2[5]) = 't'; > cout << test2; > > } >
Incidentally, we can compile this substitution for string_ex.cc
-------------------------------------------------- #include #include "test.h" using namespace std; void stringexample(){
char test[] = "My Dog has Fleas\n"; const char * test2 = "My Dog has Fleas\n"; char * test3; cout << test; test[3] = 'C'; test[4] = 'a'; test[5] = 't'; cout << test; cout << test2; test3 = const_cast(test2); test3[3] = 'C'; test3[4] = 'a'; test3[5] = 't'; cout << test2; } ---------------------------------------------
but it creates a segmentation fault on the line:
test3[3] = 'C';
if you add the compiler options -ggdb to your g++ commands in your makefile, you can trace this error. This UNDERSCORES how dangerous that explicit casting can be, even when allowed.
Ruben
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
? Copyright for the Digital Millennium
|
|