Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A funny thing happened to me. Who can explain?

A funny thing happened to me. Who can explain?

Scheduled Pinned Locked Moved C / C++ / MFC
question
34 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N nguyenvhn

    When doing my homework, I had this code: .... int main(){ const int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } Here is the output: p=0012FF7C &a=0012FF7C *p=100 a=10 i=10 I guessed that a had to be 100, but it's 10. It seem that compiler has replaced all instance of variable a to 10 at compile time, event if a has been allocated a storage. So the trick to change a constant by casting its pointer is not effect. Is that right? Any comment is welcome. Thanks.

    C Offline
    C Offline
    Christopher Lloyd
    wrote on last edited by
    #14

    Well it gives a==100 on my compiler! But i==10. I'd imagine that the problem was set to explain why i != a. You're right that the compiler effectively replaces all references to a with the value 10, and that's why i == 10 (because the compiler sees this line as being i = 10) but the value a does have a physical address and using the two lines: int* p=(int*)&a; *p=100; should change a. If I was you I'd debug the code and then look at the disassembly, there everything should become clear. In my compiler it gives this: 1844: const int a=10; 0050D1BF mov dword ptr [a],0Ah 1845: int* p=(int*)&a; 0050D1C6 lea eax,[a] 0050D1C9 mov dword ptr [p],eax 1846: *p=100; 0050D1CC mov ecx,dword ptr [p] 0050D1CF mov dword ptr [ecx],64h 1847: int i=a; 0050D1D5 mov dword ptr [i],0Ah Here it's clear that a will be changed to 100 in line 1846 and also that i will be set to 10 in line 1847. By the way, const isn't magic, it just tells the compiler that you shouldn't be able to change a using a line like a = 20; it doesn't add some kind of 'lock' to a meaning it can never change. Oh yeah, I've just spotted why a is 10 in your output. Because you output a which has been replaced at compile time with 10!!

    C A N 3 Replies Last reply
    0
    • T toxcct

      what do you think "const" mean ? constipated ? for me (and for C/C++ also), it means "constant", that is, you cannot modify it ! that's all....


      TOXCCT >>> GEII power

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #15

      Sounds like your constipated!!!!!:mad:

      T 1 Reply Last reply
      0
      • C Cedric Moonen

        Yes of course I know this (I'm not completely stupid ;P) but if you try to change the contents of it, you will have a compile error. Or here, you change it by another way (using the adress of the variable). And there is no errors BUT the adress p and the adress &a are the same but their contents are different so there is something strange (two pointers that are pointing at exactly the same adress but the values they hold are different X| )!! It would be normal if *p still contain 10 at the end (that menas the content of a was unchanged)

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #16

        in fact, the compiler don't say anything because const is an identifier for the compiler, and as we don't explicitely write into variable a, there is no compile error. but a is still const (certainly not even in the stack...) so *p cannot access the value correctly to alter it.


        TOXCCT >>> GEII power

        1 Reply Last reply
        0
        • A Anonymous

          Sounds like your constipated!!!!!:mad:

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #17

          hey man, first, you write as an anonymous, and secondly, where did you sense of humour go ??? did you trash it ? :-D


          TOXCCT >>> GEII power

          1 Reply Last reply
          0
          • T toxcct

            what do you think "const" mean ? constipated ? for me (and for C/C++ also), it means "constant", that is, you cannot modify it ! that's all....


            TOXCCT >>> GEII power

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #18

            Sounds like your are constipated!!!!:mad:

            T 1 Reply Last reply
            0
            • C Christopher Lloyd

              Well it gives a==100 on my compiler! But i==10. I'd imagine that the problem was set to explain why i != a. You're right that the compiler effectively replaces all references to a with the value 10, and that's why i == 10 (because the compiler sees this line as being i = 10) but the value a does have a physical address and using the two lines: int* p=(int*)&a; *p=100; should change a. If I was you I'd debug the code and then look at the disassembly, there everything should become clear. In my compiler it gives this: 1844: const int a=10; 0050D1BF mov dword ptr [a],0Ah 1845: int* p=(int*)&a; 0050D1C6 lea eax,[a] 0050D1C9 mov dword ptr [p],eax 1846: *p=100; 0050D1CC mov ecx,dword ptr [p] 0050D1CF mov dword ptr [ecx],64h 1847: int i=a; 0050D1D5 mov dword ptr [i],0Ah Here it's clear that a will be changed to 100 in line 1846 and also that i will be set to 10 in line 1847. By the way, const isn't magic, it just tells the compiler that you shouldn't be able to change a using a line like a = 20; it doesn't add some kind of 'lock' to a meaning it can never change. Oh yeah, I've just spotted why a is 10 in your output. Because you output a which has been replaced at compile time with 10!!

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #19

              Finally it's clear :-D !! Yes ok, I didn't know that the compiler really replace all occurences of a by it's value (so acting like a #define in fact). What did you win ;P ??

              1 Reply Last reply
              0
              • A Anonymous

                Sounds like your are constipated!!!!:mad:

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #20

                pouet pouet pouet traallalalilouuu


                TOXCCT >>> GEII power

                1 Reply Last reply
                0
                • N nguyenvhn

                  When doing my homework, I had this code: .... int main(){ const int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } Here is the output: p=0012FF7C &a=0012FF7C *p=100 a=10 i=10 I guessed that a had to be 100, but it's 10. It seem that compiler has replaced all instance of variable a to 10 at compile time, event if a has been allocated a storage. So the trick to change a constant by casting its pointer is not effect. Is that right? Any comment is welcome. Thanks.

                  A Offline
                  A Offline
                  Antony M Kancidrowski
                  wrote on last edited by
                  #21

                  OK seriously, You should not cast away the const-ness of objects that were originally defined as being const and on which non-const operations are being executed. Doing this, results in undefined behaviour. http://www.uwyn.com/resources/uwyn_cpp_coding_standard/x629.html[^] Ant.

                  1 Reply Last reply
                  0
                  • T toxcct

                    where do you see a difference between *(int*)&a and a you, except in confusing the reader ?? :~


                    TOXCCT >>> GEII power

                    A Offline
                    A Offline
                    Antony M Kancidrowski
                    wrote on last edited by
                    #22

                    Try it in VC 6 you will be amazed, anyway see my other post WRT casting away const-ness. Ant.

                    1 Reply Last reply
                    0
                    • C Christopher Lloyd

                      Well it gives a==100 on my compiler! But i==10. I'd imagine that the problem was set to explain why i != a. You're right that the compiler effectively replaces all references to a with the value 10, and that's why i == 10 (because the compiler sees this line as being i = 10) but the value a does have a physical address and using the two lines: int* p=(int*)&a; *p=100; should change a. If I was you I'd debug the code and then look at the disassembly, there everything should become clear. In my compiler it gives this: 1844: const int a=10; 0050D1BF mov dword ptr [a],0Ah 1845: int* p=(int*)&a; 0050D1C6 lea eax,[a] 0050D1C9 mov dword ptr [p],eax 1846: *p=100; 0050D1CC mov ecx,dword ptr [p] 0050D1CF mov dword ptr [ecx],64h 1847: int i=a; 0050D1D5 mov dword ptr [i],0Ah Here it's clear that a will be changed to 100 in line 1846 and also that i will be set to 10 in line 1847. By the way, const isn't magic, it just tells the compiler that you shouldn't be able to change a using a line like a = 20; it doesn't add some kind of 'lock' to a meaning it can never change. Oh yeah, I've just spotted why a is 10 in your output. Because you output a which has been replaced at compile time with 10!!

                      A Offline
                      A Offline
                      Antony M Kancidrowski
                      wrote on last edited by
                      #23

                      Yea it seems that the compiler has optimised itself here since a was declared const! :) Ant.

                      1 Reply Last reply
                      0
                      • N nguyenvhn

                        When doing my homework, I had this code: .... int main(){ const int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } Here is the output: p=0012FF7C &a=0012FF7C *p=100 a=10 i=10 I guessed that a had to be 100, but it's 10. It seem that compiler has replaced all instance of variable a to 10 at compile time, event if a has been allocated a storage. So the trick to change a constant by casting its pointer is not effect. Is that right? Any comment is welcome. Thanks.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #24

                        nguyenvhn wrote: const int a=10; This simply means that you cannot modify memory address 0x0012FF7C through variable a. The address is not read-only, the variable is. make sense?


                        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                        T 1 Reply Last reply
                        0
                        • D David Crow

                          nguyenvhn wrote: const int a=10; This simply means that you cannot modify memory address 0x0012FF7C through variable a. The address is not read-only, the variable is. make sense?


                          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                          T Offline
                          T Offline
                          toxcct
                          wrote on last edited by
                          #25

                          teu teu teu, read the other posts before saying so, especially "Christopher Lloyd"'s one :(


                          TOXCCT >>> GEII power

                          D 1 Reply Last reply
                          0
                          • T toxcct

                            teu teu teu, read the other posts before saying so, especially "Christopher Lloyd"'s one :(


                            TOXCCT >>> GEII power

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #26

                            In which case he states that "it just tells the compiler that you shouldn't be able to change a using a line like a = 20" In my post, I stated that "you cannot modify memory address 0x0012FF7C through variable a" What's your point?


                            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                            T 1 Reply Last reply
                            0
                            • D David Crow

                              In which case he states that "it just tells the compiler that you shouldn't be able to change a using a line like a = 20" In my post, I stated that "you cannot modify memory address 0x0012FF7C through variable a" What's your point?


                              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                              T Offline
                              T Offline
                              toxcct
                              wrote on last edited by
                              #27

                              yes you can const is only a specification for the compiler!!! if you had read all the posts here, you would have seen that we already said that also... and i even that i was thinking like you ! :mad:


                              TOXCCT >>> GEII power

                              D 1 Reply Last reply
                              0
                              • T toxcct

                                yes you can const is only a specification for the compiler!!! if you had read all the posts here, you would have seen that we already said that also... and i even that i was thinking like you ! :mad:


                                TOXCCT >>> GEII power

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #28

                                Then how would you propose changing the value through variable a?


                                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                T 1 Reply Last reply
                                0
                                • D David Crow

                                  Then how would you propose changing the value through variable a?


                                  "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                  T Offline
                                  T Offline
                                  toxcct
                                  wrote on last edited by
                                  #29

                                  by a pointer to its address. int* p = &a;. this works, but badly, the compiler replaced each occurence of a by its value (10 here). that is why, even if the value contained at the adress of a is changed, the compiler used each a uses as a #define. int d = a; will affect 10 to d, even if the line before was *p = 100;. Do we now understand better ?


                                  TOXCCT >>> GEII power

                                  D 1 Reply Last reply
                                  0
                                  • T toxcct

                                    by a pointer to its address. int* p = &a;. this works, but badly, the compiler replaced each occurence of a by its value (10 here). that is why, even if the value contained at the adress of a is changed, the compiler used each a uses as a #define. int d = a; will affect 10 to d, even if the line before was *p = 100;. Do we now understand better ?


                                    TOXCCT >>> GEII power

                                    D Offline
                                    D Offline
                                    David Crow
                                    wrote on last edited by
                                    #30

                                    int *p = &a;
                                    *p = 100;

                                    This does not change the value through variable a. The value is changed through variable p. Do you still find my original "...you cannot modify memory address 0x0012FF7C through variable a. The address is not read-only, the variable is" statement to be false. I *think* we are saying the same thing, so I'm not quite sure why this thread is six posts longer than necessary.


                                    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                    1 Reply Last reply
                                    0
                                    • N nguyenvhn

                                      When doing my homework, I had this code: .... int main(){ const int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } Here is the output: p=0012FF7C &a=0012FF7C *p=100 a=10 i=10 I guessed that a had to be 100, but it's 10. It seem that compiler has replaced all instance of variable a to 10 at compile time, event if a has been allocated a storage. So the trick to change a constant by casting its pointer is not effect. Is that right? Any comment is welcome. Thanks.

                                      V Offline
                                      V Offline
                                      vividtang
                                      wrote on last edited by
                                      #31

                                      #include using namespace std; int main(){ int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } the running result p=0012FF7C &a=0012FF7C *p=100 a=100 i=100

                                      A 1 Reply Last reply
                                      0
                                      • N nguyenvhn

                                        When doing my homework, I had this code: .... int main(){ const int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } Here is the output: p=0012FF7C &a=0012FF7C *p=100 a=10 i=10 I guessed that a had to be 100, but it's 10. It seem that compiler has replaced all instance of variable a to 10 at compile time, event if a has been allocated a storage. So the trick to change a constant by casting its pointer is not effect. Is that right? Any comment is welcome. Thanks.

                                        A Offline
                                        A Offline
                                        Anand Paranjpe
                                        wrote on last edited by
                                        #32

                                        this is because of compiler optimization. Just have a look at assembly code. const int a=10; 0041B1CE mov dword ptr [a],0Ah int* p=(int*)&a; 0041B1D5 lea eax,[a] 0041B1D8 mov dword ptr [p],eax *p=100; 0041B1DB mov eax,dword ptr [p] 0041B1DE mov dword ptr [eax],64h int i=a; 0041B1E4 mov dword ptr [i],0Ah Also while printing on console... cout << "a=" << a << endl; 0041B291 push offset std::endl (4194BAh) 0041B296 push 0Ah 0041B298 push offset string "a=" (44E0C8h) 0041B29D push offset std::cout (457668h) 0041B2A2 call std::operator<< > (419A96h) 0041B2A7 add esp,8 0041B2AA mov ecx,eax 0041B2AC call std::basic_ostream >::operator<< (4195DCh) 0041B2B1 mov ecx,eax 0041B2B3 call std::basic_ostream >::operator<< (419AC3h) You will not experience this behavour with all the compilers. Thanks, Anand The chosen One :)

                                        1 Reply Last reply
                                        0
                                        • V vividtang

                                          #include using namespace std; int main(){ int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } the running result p=0012FF7C &a=0012FF7C *p=100 a=100 i=100

                                          A Offline
                                          A Offline
                                          Anand Paranjpe
                                          wrote on last edited by
                                          #33

                                          why are you tampering code by removing const while declaring variable a. it should be const int a=10; The chosen One :)

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups