Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

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

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    seeing as x is a constant I think you will be finding it hard to change its value

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    Well try it

    Try it...and you will see that the compiler does some opitimizing thats not obvious

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    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.

  6. #5
    Just Joined!
    Join Date
    Dec 2007
    Posts
    5
    Quote Originally Posted by coopstah13 View Post
    seeing as x is a constant I think you will be finding it hard to change its value
    coopstah is correct, and so is the compiler.

    Make the x non-constant and you will see that changing y* also changes x.

  7. #6
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    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

  8. #7
    Just 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.

  9. #8
    oz
    oz is offline
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,733

    zombie thread

    Thread is nearing 3 years old... locking it down.
    oz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •