Results 1 to 8 of 8
//compile and run this code
//the compiler will opitmize it and produce unexpected results
//found this interesting...thought you might also
#include <iostream>
int main(int argc, char**argv)
{
const int x ...
- 12-28-2007 #1
gnu c/c++ compiler oddities
//compile and run this code
//the compiler will opitmize it and produce unexpected results
//found this interesting...thought you might also
#include <iostream>
int main(int argc, char**argv)
{
const int x = 1234;
int *y = (int*)&x;
//y now points to x as code below will show
std::cout<<"x->\t\t"<<x<<"\n";
std::cout<<"addres of x->\t"<<&x<<"\n";
std::cout<<"*y->\t\t"<<*y<<"\n";
std::cout<<"address of y->\t"<<y<<"\n";
//now change the value y points to
//and you will see x still equals 1234
//but now y equals 98765
*y = 98765;
std::cout<<"x->\t\t"<<x<<"\n";
std::cout<<"addres of x->\t"<<&x<<"\n";
std::cout<<"*y->\t\t"<<*y<<"\n";
std::cout<<"address of y->\t"<<y<<"\n";
//to see why this happens look a the the assmembler produced by
//by this file -> g++ -S filename.cpp.
//I love these little compiler oddities
return 0;
}
- 12-28-2007 #2
seeing as x is a constant I think you will be finding it hard to change its value
- 12-28-2007 #3
Well try it
Try it...and you will see that the compiler does some opitimizing thats not obvious
- 12-28-2007 #4
I'm not a compiler expert, but I would say for this reason you are showing is why it would make sense they would have this output. Since you can still change the value at that address the compiler probably keeps the value of all constants on the stack rather than in the heap.
- 12-29-2007 #5Just Joined!
- Join Date
- Dec 2007
- Posts
- 5
- 12-29-2007 #6
You missed the point
Because x is a constant the compiler will optimize the code by writing the value directly into the instructions. So if you set y pointer equal to address of const x you force the compiler to creat a label for x. Now you have the value of const x hard coded into the the instructions
and a label for const x. When you cast const x as an int we can change the value of it, but since the value of const x is hard coded into the intructions it appears that we have two values residing in one four bytes memory location...just some compiler magic
You can see this in the assembler if you compile the above code g++ -S filename.cpp
and look at the results
- 09-26-2010 #7Just Joined!
- Join Date
- Sep 2010
- Posts
- 1
The line
int *y = (int*)&x;
is illegal. It should read
const int *y = (const int*)&x;
The compiler probably accepts it for compatibility with C.
EDIT: with
const int &x = 1234;
instead of
const int x = 1234;
the value of x does change along with that of *y.Last edited by brac37; 09-26-2010 at 07:16 PM.
- 09-26-2010 #8forum.guy
- Join Date
- May 2004
- Location
- arch linux
- Posts
- 18,096
zombie thread
Thread is nearing 3 years old... locking it down.
oz
→ new members/users: read this first | new member faq
→ no private messages requesting computer support - post them on the forums!
→ please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.



