C++ breached...

R

rajeshdotcom

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
 
H

Howard

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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

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++.
 
N

Noah Roberts

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.
 
F

Frederick Gotham

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".
 
F

Frederick Gotham

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top