By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,463 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,463 IT Pros & Developers. It's quick & easy.

C++ breached...

P: n/a
C++ breached...Here are samples tried to breach C++ constructs with no
intension to criticize the best programming language on earth.

-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
[Sample.1] Say no to mutable...here is a way to assign a value to data
member from a const member function.

/* Featured */
class bike
{
public:
int bikenumber;
bike():bikenumber(0) {
}
void newnumber() const {
// bikenumber need to be mutable
bikenumber = 1143; // error C2166
}
};

int main(int argc, char* argv[]) {
bike mybike;
mybike.newnumber();

return 0;
}

/* Violated */
class bike
{
public:
int bikenumber;
bike():bikenumber(0) {
}
void newnumber() const {
int *pointertrick = (int *)&bikenumber;
*pointertrick = 1143;
}
};

int main(int argc, char* argv[]) {
bike mybike;
mybike.newnumber();

return 0;
}

-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
Sample.2. Constants are modifiable...

/* Featured */
const int bikenumber = 3411;
bikenumber = 1143; // error C2166

/* Violated */
const int bikenumber = 3411;
int* newnumber = (int *)&bikenumber;
*newnumber = 1143;

-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
Sample.3. Constant pointers are modifiable...

/* Featured */
int bikenumber1 = 1003;
int bikenumber2 = 1143;
int* const newnumber = &bikenumber1;

newnumber = &bikenumber2; // error C2166

/* Violated */
int bikenumber1 = 1003;
int bikenumber2 = 1143;
int* const newnumber = &bikenumber1;

int** indirect = (int **)&newnumber;
*indirect = (int *)&bikenumber2; // ref modified

rajesh`b <
Sep 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

<ra**********@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
C++ breached...Here are samples tried to breach C++ constructs with no
intension to criticize the best programming language on earth.
And your point is...? C++ is not designed to be fool-proof. If you want to
do something illegal, you're free to do so. Just don't expect that you'll
get the behavior you expect when doing so. For example, writing code to
modify a const object via a pointer is easy to do, as you've shown, but
executing that code constitutes "Undefined Behavior". As such, there is no
guarantee whatsoever what will happen when you do so. It may work,
sometimes or always. It may crash, sometimes or always. It may do
unexpected things. [Pick your favorite unexpected event here. I like
demons flying out one's nose, personally.]. The behavior is, as defined,
undefined. :-)

-Howard
Sep 14 '06 #2

P: n/a
ra**********@hotmail.com wrote:
C++ breached...Here are samples tried to breach C++ constructs with no
intension to criticize the best programming language on earth.
You are breaking your own code, not C++.

--
Salu2
Sep 14 '06 #3

P: n/a

ra**********@hotmail.com wrote:
C++ breached...Here are samples tried to breach C++ constructs with no
intension to criticize the best programming language on earth.

-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
[Sample.1] Say no to mutable...here is a way to assign a value to data
member from a const member function.
/* Violated */
class bike
{
public:
int bikenumber;
bike():bikenumber(0) {
}
void newnumber() const {
int *pointertrick = (int *)&bikenumber;
*pointertrick = 1143;
}
};
This is why one should avoid C style casts. Note that the above causes
undefined behavior but the compiler will illicit no warning at all.
What you have here is a const_cast and any time a const_cast appears in
code (to cast AWAY constness) you can bet that constructs causing
undefined behavior follow.

In this case C++ is not "breached". We have an example of a program
with no defined result.
-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
Sample.2. Constants are modifiable...

/* Featured */
const int bikenumber = 3411;
bikenumber = 1143; // error C2166

/* Violated */
const int bikenumber = 3411;
int* newnumber = (int *)&bikenumber;
*newnumber = 1143;
Again, const_cast followed by constructs causing undefined behavior.
This one is very likely to result in a crash in many implementations
but doesn't have to.
int** indirect = (int **)&newnumber;
*indirect = (int *)&bikenumber2; // ref modified
Same.

Yes, you can cast away constness. This should never be done and almost
always results in behavior that is undefined. The cast itself is not
the problem, modifying the values is and is defined by the standard as
having no definable result.

Note that you have the same problem with the following case:

char * x = "hello";

x[1] = 'i';

Constness is actually cast away in the above without any explicit
request to and without any warning whatsoever.

Sep 14 '06 #4

P: n/a
int *pointertrick = (int *)&bikenumber;
*pointertrick = 1143;

Do a Google search for "Undefined Behaviour".

const int bikenumber = 3411;
int* newnumber = (int *)&bikenumber;
*newnumber = 1143;

Do a Google search for "Undefined Behaviour".

/* Violated */
int bikenumber1 = 1003;
int bikenumber2 = 1143;
int* const newnumber = &bikenumber1;

int** indirect = (int **)&newnumber;
*indirect = (int *)&bikenumber2; // ref modified

Do a Google search for "Undefined Behaviour".

--

Frederick Gotham
Sep 14 '06 #5

P: n/a
Noah Roberts posted:
Note that the above causes
undefined behavior but the compiler will illicit no warning at all.

All the compilers I've ever used _do_.

What you have here is a const_cast and any time a const_cast appears in
code (to cast AWAY constness) you can bet that constructs causing
undefined behavior follow.

int i;

int const *const p = &i;

int *q = const_cast<int*>(p);

*q = 5;

There's plenty of ways to use const_cast without invoking undefined behaviour
-- if they're weren't, it wouldn't be in the language.

--

Frederick Gotham
Sep 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.