473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C++ breached...

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
5 1606

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: CJM | last post by:
I'm building a search function for one of my applications. The user has the option to enter a number criteria of criteria, but none are compulsary. I need to be able to build up a query string that...
32
by: Mike MacSween | last post by:
Further to 'Security - more complex than I thought' Has anybody ever seen any studies? Or anecdotal evidence? Done any studies themselves? Done any lab testing - you know - 10 users asked to get...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.