MESSAGE
| DATE | 2010-03-03 |
| FROM | Ruben Safir
|
| SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics - agregate data
|
From owner-hangout-outgoing-at-mrbrklyn.com Wed Mar 3 23:44:16 2010 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by www2.mrbrklyn.com (Postfix) id 32F7B53987; Wed, 3 Mar 2010 23:44:16 -0500 (EST) Delivered-To: hangout-outgoing-at-www2.mrbrklyn.com Received: by www2.mrbrklyn.com (Postfix, from userid 28) id 23E90542FB; Wed, 3 Mar 2010 23:44:16 -0500 (EST) Delivered-To: hangout-at-nylxs.com Received: from mail1.panix.com (mail1.panix.com [166.84.1.72]) by www2.mrbrklyn.com (Postfix) with ESMTP id B80AB53987 for ; Wed, 3 Mar 2010 23:44:15 -0500 (EST) Received: from panix1.panix.com (panix1.panix.com [166.84.1.1]) by mail1.panix.com (Postfix) with ESMTP id E0FC61F08A for ; Wed, 3 Mar 2010 23:45:10 -0500 (EST) Received: by panix1.panix.com (Postfix, from userid 20529) id D6A0014B98; Wed, 3 Mar 2010 23:45:10 -0500 (EST) Date: Wed, 3 Mar 2010 23:45:10 -0500 From: Ruben Safir To: hangout-at-nylxs.com Subject: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics - agregate data types Message-ID: <20100304044510.GB28579-at-panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.18 (2008-05-17) Sender: owner-hangout-at-mrbrklyn.com Precedence: bulk Reply-To: hangout-at-mrbrklyn.com
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
|
|