473,327 Members | 1,892 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,327 software developers and data experts.

Is it constant folding at work

Have a look at the following code:

#include<iostream>

using namespace std;

int main()
{
const int a = 1;
const int* const aptr1 = &a;
int* const aptr2 = const_cast<int*>aptr1);
*aptr2 = 2;
cout<<a<<endl;
cout<<*(&a)<<endl;
cout<<*aptr1<<endl;
cout<<*aptr2<<endl;
}

Output is (for g++ (ver. 3.2) and visual C++ 6 compiler)

1
1
2
2

Is it the correct behaviour?
Is it due to constant folding?
If yes, how is constant folding possible when we are referencing the variable?
Jul 22 '05 #1
7 1576
Anshul Sawant wrote in
news:bd*************************@posting.google.co m:
Have a look at the following code:

#include<iostream>

using namespace std;

int main()
{
const int a = 1;
const int* const aptr1 = &a;
This cast if fine and legal (ok you missed a '('), but ...
int* const aptr2 = const_cast<int*>aptr1);
The use of the result const_cast here is UB (uderfined Behaviour),
you should only use const_cast when you *know* the original thing
pointed to wasn't a constant.
*aptr2 = 2;
All bets are now Officialy (according to The C++ Standard) off.
cout<<a<<endl;
cout<<*(&a)<<endl;
cout<<*aptr1<<endl;
cout<<*aptr2<<endl;
}

Output is (for g++ (ver. 3.2) and visual C++ 6 compiler)

1
1
2
2

Is it the correct behaviour?
Yup, but so is you computer growing leggs and wandering off to
make a cup of tea.
Is it due to constant folding?
Its due to underfined behaviour.
If yes, how is constant folding possible when we are referencing the
variable?


HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
> int* const aptr2 = const_cast<int*>aptr1);
A int* const aptr2 doe not mean that the object at which aptr2 points is
const but it means that the pointer itself cannot be rebound to another
object. The object itself can be changed throught that pointer. So this line
*aptr2 = 2;


is allowed to thange the object. I do not think that the compiler can catch
that.

From a previous post:
constness for pointers
// not const at all
A. TestClass * ptr;

// non-const pointer to a const object
B. const TestClass * ptr_to_const;

// const pointer to non-const object
C. TestClass * const const_ptr;

// const pointer to const object
D. const TestClass * const const_ptr_to_const;


Jul 22 '05 #3

"Anshul Sawant" <an***********@read-ink.com> wrote in message
news:bd*************************@posting.google.co m...
Have a look at the following code:

#include<iostream>

using namespace std;

int main()
{
const int a = 1;
const int* const aptr1 = &a;
To a decent compiler this is just as constant as the first one.
int* const aptr2 = const_cast<int*>aptr1);
*aptr2 = 2;
This is undefined behaviour - it could easily case a core dump on some
architectures.
cout<<a<<endl;
I would expect every compiler to print 1 for this.
cout<<*(&a)<<endl;
I am surprised that they were good enough to optimize this away.
cout<<*aptr1<<endl; cout<<*aptr2<<endl;
}

Output is (for g++ (ver. 3.2) and visual C++ 6 compiler)

1
1
2
2

Is it the correct behaviour?
There is no correct behaviour defined.
Is it due to constant folding?
No it's due to optimization.
If yes, how is constant folding possible when we are referencing the

variable?

Be careful about taking the address of constants -
If you put a constant in a header and take its address in 2 different source
files the addresses will be different!
constant have local scope by default.
If you ever need to do this you must use extern.
Jul 22 '05 #4
Have a look at the following code:

#include<iostream>

using namespace std;

int main()
{
const int a = 1;
const int* const aptr1 = &a;
int* const aptr2 = const_cast<int*>aptr1);
*aptr2 = 2;
cout<<a<<endl;
cout<<*(&a)<<endl;
cout<<*aptr1<<endl;
cout<<*aptr2<<endl;
}

Output is (for g++ (ver. 3.2) and visual C++ 6 compiler)

1
1
2
2

If ( aptr1 == &a ), how is it possible that " *aptr != *(&a)" ?
confusing....
Jul 22 '05 #5
In article <Pi******************************@speyburn.esat.ku leuven.ac.be>, Xiaobin Yang wrote:
Have a look at the following code:

#include<iostream>

using namespace std;

int main()
{
const int a = 1;
const int* const aptr1 = &a;
int* const aptr2 = const_cast<int*>aptr1);
*aptr2 = 2;
cout<<a<<endl;
cout<<*(&a)<<endl;
cout<<*aptr1<<endl;
cout<<*aptr2<<endl;
}

Output is (for g++ (ver. 3.2) and visual C++ 6 compiler)

1
1
2
2

If ( aptr1 == &a ), how is it possible that " *aptr != *(&a)" ?
confusing....


a is declared as a const int. This means to the compiler that the value
of a can never change. Knowing that, the compiler just sends the value
1 to cout when it finds a or *(&a).

Exchanging the cout << X << endl whith printf("%d\n", X), g++ 3.3.3
emits:
movl $1, 4(%esp)
movl $.LC0, (%esp)
call printf
for the first calls to printf. Which says: Put the value 1 on the
stack; put the value .LC0 (.LC0 is a pointer to "%d\n") on the stack;
call printf.

const int a = 1;
generates
movl $1, -4(%ebp)
put the value 1 in memory located by -4(%ebp)

const int* const aptr1 = &a;
generates
leal -4(%ebp), %eax
load the effective address of -4(%ebp) (== &a) into %eax
movl %eax, -8(%ebp)
move the contents of %eax into memory -8(%ebp) (==aptr1)

int* const aptr2 = const_cast<int*>(aptr1);
generates
movl -8(%ebp), %eax
move the value of -8(%ebp) (==aptr1) int %eax
movl %eax, -12(%ebp)
move the value of %eax into memory -12(%ebp) (==aptr2)

*aptr2 = 2
generates
movl -12(%ebp), %eax
move the value of -12(%ebp) (==aptr2) into %eax
movl $2, (%eax)
put the value 2 into the address %eax points to (==*aptr2)

Now the value of the object a is 2, but the compiler nevertheless uses
the value 1 when it finds a in the source code. Lesson: casting away
constness doesn't always work.
--
Robert Bauck Hamar
Jul 22 '05 #6
Thanks! I see, it's the compiler that consistently replace "a" with 1.
In this sense, "const" is a little like MACRO.

On Mon, 7 Jun 2004, Robert Bauck Hamar wrote:
If ( aptr1 == &a ), how is it possible that " *aptr != *(&a)" ?
confusing....


a is declared as a const int. This means to the compiler that the value
of a can never change. Knowing that, the compiler just sends the value
1 to cout when it finds a or *(&a).

Exchanging the cout << X << endl whith printf("%d\n", X), g++ 3.3.3
emits:
movl $1, 4(%esp)
movl $.LC0, (%esp)
call printf
for the first calls to printf. Which says: Put the value 1 on the
stack; put the value .LC0 (.LC0 is a pointer to "%d\n") on the stack;
call printf.

const int a = 1;
generates
movl $1, -4(%ebp)
put the value 1 in memory located by -4(%ebp)

const int* const aptr1 = &a;
generates
leal -4(%ebp), %eax
load the effective address of -4(%ebp) (== &a) into %eax
movl %eax, -8(%ebp)
move the contents of %eax into memory -8(%ebp) (==aptr1)

int* const aptr2 = const_cast<int*>(aptr1);
generates
movl -8(%ebp), %eax
move the value of -8(%ebp) (==aptr1) int %eax
movl %eax, -12(%ebp)
move the value of %eax into memory -12(%ebp) (==aptr2)

*aptr2 = 2
generates
movl -12(%ebp), %eax
move the value of -12(%ebp) (==aptr2) into %eax
movl $2, (%eax)
put the value 2 into the address %eax points to (==*aptr2)

Now the value of the object a is 2, but the compiler nevertheless uses
the value 1 when it finds a in the source code. Lesson: casting away
constness doesn't always work.
--
Robert Bauck Hamar


-- xiaobin
Jul 22 '05 #7
Xiaobin Yang wrote:

Thanks! I see, it's the compiler that consistently replace "a" with 1.


No. It *may* do this. It doesn't need to.

The conclusio is this:
Don't lie to your compiler!

If you tell it: "Hey buddy, this is constant. 'a' has a value
of 1 and I will never change that to anything else" you should
not attempt to do exactly that: Change it to anything else.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nuff Said | last post by:
Question: Is there a way to unfold a Python class *without* unfolding all its methods etc. so that you can get a quick overview of the class? Details: I normally open a file either with...
0
by: Terry Hancock | last post by:
My general attitude towards IDEs and editors has been extremely conservative, but today I decided to see what this "folding" business was all about. I see that vim (and gvim, which is what I...
20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
5
by: robert.h.lowe | last post by:
Hi, I'm looking for a computationally fast means of XOR folding the individual bytes of an unsigned 32-bit int, and an unsigned 16-bit int. Can I point a char * at the address of each of the...
14
by: John Salerno | last post by:
Specifically, I'm using UltraEdit and perhaps there's no way perfect way to implement code folding with it, given how it uses its syntax highlighting file to do so (i.e., you have to specify an...
22
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved...
34
by: jacob navia | last post by:
Hi I am adding an optimization to lcc-win: sqrt(2.0) will provoke now that the constant 1.4142... etc will be generated instead of generating an actual call. Details: -------
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.