Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Ambiguity when non constant pointer is pointing to constant variable
QtWS25 Last Chance

Ambiguity when non constant pointer is pointing to constant variable

Scheduled Pinned Locked Moved Unsolved C++ Gurus
12 Posts 7 Posters 1.1k Views
  • 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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by
    #1

    Hi All,

    pointer_issue.png

    1. In the code above, a non constant pointer is pointing to constant variable.
    2. I was trying to change the variable value using *ptr = 100.
    3. When I check the value of *ptr and a, it was different. (Line number 20 and 21)
    4. But both ptr and &a is pointing to same memory location. (Line number 23)

    I am not able to understand how, two different values are assigned to same memory address ? does compiler creates temporary address to hold both different values here (100 and 10) ?

    Any help is appreciated. Thanks!

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • V Vinoth Rajendran4

      Hi All,

      pointer_issue.png

      1. In the code above, a non constant pointer is pointing to constant variable.
      2. I was trying to change the variable value using *ptr = 100.
      3. When I check the value of *ptr and a, it was different. (Line number 20 and 21)
      4. But both ptr and &a is pointing to same memory location. (Line number 23)

      I am not able to understand how, two different values are assigned to same memory address ? does compiler creates temporary address to hold both different values here (100 and 10) ?

      Any help is appreciated. Thanks!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Vinoth-Rajendran4
      I believe your code does write into a via *ptr. I wonder if the compiler has kept const int a in a register, does not expect it to be changed since it's const, and reports the original value for cout << a?

      I wonder if you can fool it with cout << *&a?

      V 1 Reply Last reply
      0
      • V Vinoth Rajendran4

        Hi All,

        pointer_issue.png

        1. In the code above, a non constant pointer is pointing to constant variable.
        2. I was trying to change the variable value using *ptr = 100.
        3. When I check the value of *ptr and a, it was different. (Line number 20 and 21)
        4. But both ptr and &a is pointing to same memory location. (Line number 23)

        I am not able to understand how, two different values are assigned to same memory address ? does compiler creates temporary address to hold both different values here (100 and 10) ?

        Any help is appreciated. Thanks!

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Vinoth-Rajendran4 thats not what const canst was defined for, and is undefined behaviour, switch the compiler, optimisation level or to release/debug build, you may get a different outcome.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        V 1 Reply Last reply
        1
        • JonBJ JonB

          @Vinoth-Rajendran4
          I believe your code does write into a via *ptr. I wonder if the compiler has kept const int a in a register, does not expect it to be changed since it's const, and reports the original value for cout << a?

          I wonder if you can fool it with cout << *&a?

          V Offline
          V Offline
          Vinoth Rajendran4
          wrote on last edited by
          #4

          @JonB : for cout << *&a , I am getting value as 100.

          KroMignonK JonBJ 2 Replies Last reply
          0
          • V Vinoth Rajendran4

            @JonB : for cout << *&a , I am getting value as 100.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @Vinoth-Rajendran4 said in Ambiguity when non constant pointer is pointing to constant variable:

            for cout << *&a , I am getting value as 100

            As @J-Hilk already said, your code is almost unsafe. Depending on C++ compiler type (GCC, MSVC, etc.), code optimization level and build mode (release/debug) you will probably got different results.

            Declaring a variable as const will allow compiler to do some optimization. Violate this const state will introduce unpredictable behavior.

            It is not because you can write a code that it implies that this code is valid, for example following code is also valid but will crash:

            char * ptr = nullptr;
            
            cout << ptr;
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            fcarneyF 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @Vinoth-Rajendran4 thats not what const canst was defined for, and is undefined behaviour, switch the compiler, optimisation level or to release/debug build, you may get a different outcome.

              V Offline
              V Offline
              Vinoth Rajendran4
              wrote on last edited by
              #6

              @J-Hilk : I thought const_cast<> is used to remove or add constant to the expression. If its okay, can you please explain me the actual use of const_cast<> ?

              KroMignonK jsulmJ 2 Replies Last reply
              0
              • V Vinoth Rajendran4

                @J-Hilk : I thought const_cast<> is used to remove or add constant to the expression. If its okay, can you please explain me the actual use of const_cast<> ?

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @Vinoth-Rajendran4 said in Ambiguity when non constant pointer is pointing to constant variable:

                I thought const_cast<> is used to remove or add constant to the expression. If its okay, can you please explain me the actual use of const_cast<> ?

                The easiest is to look at C++ documentation (cf. https://en.cppreference.com/w/cpp/language/const_cast):

                const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to a const object or a reference or pointer to non-volatile type that is actually referring to a volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                4
                • V Vinoth Rajendran4

                  @JonB : for cout << *&a , I am getting value as 100.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Vinoth-Rajendran4 said in Ambiguity when non constant pointer is pointing to constant variable:

                  @JonB : for cout << *&a , I am getting value as 100.

                  Which was my point/guess! At some level cout << a is taking advantage of the const int a = 10; it saw earlier, it's not really going to fetch the current contents of a which you actually altered via *ptr = 100. Some optimization (on your compiler) decided you would not alter a.

                  My guess was that *&a would force it to generate code to actually look in the a memory address for the value, by-passing the "optimization". And so it has proved to be --- again, on your compiler.

                  As the others have said, this whole thing is compiler-specific/undefined/"very naughty", for precisely the above reasons (e.g. see @KroMignon's quote) and the way you are seeing a != *&a. Which is why you shouldn't be doing it! :)

                  1 Reply Last reply
                  1
                  • V Vinoth Rajendran4

                    @J-Hilk : I thought const_cast<> is used to remove or add constant to the expression. If its okay, can you please explain me the actual use of const_cast<> ?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Vinoth-Rajendran4 See here: https://en.cppreference.com/w/cpp/language/const_cast
                    What you are doing here is exactly what is mentioned there: "Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior."

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    6
                    • KroMignonK KroMignon

                      @Vinoth-Rajendran4 said in Ambiguity when non constant pointer is pointing to constant variable:

                      for cout << *&a , I am getting value as 100

                      As @J-Hilk already said, your code is almost unsafe. Depending on C++ compiler type (GCC, MSVC, etc.), code optimization level and build mode (release/debug) you will probably got different results.

                      Declaring a variable as const will allow compiler to do some optimization. Violate this const state will introduce unpredictable behavior.

                      It is not because you can write a code that it implies that this code is valid, for example following code is also valid but will crash:

                      char * ptr = nullptr;
                      
                      cout << ptr;
                      
                      fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #10

                      @KroMignon said in Ambiguity when non constant pointer is pointing to constant variable:

                      cout << ptr;

                      This won't crash. It will just print 0. But this: cout<< *ptr; will.

                      C++ is a perfectly valid school of magic.

                      KroMignonK 1 Reply Last reply
                      2
                      • Kent-DorfmanK Offline
                        Kent-DorfmanK Offline
                        Kent-Dorfman
                        wrote on last edited by Kent-Dorfman
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • fcarneyF fcarney

                          @KroMignon said in Ambiguity when non constant pointer is pointing to constant variable:

                          cout << ptr;

                          This won't crash. It will just print 0. But this: cout<< *ptr; will.

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #12

                          @fcarney said in Ambiguity when non constant pointer is pointing to constant variable:

                          This won't crash. It will just print 0. But this: cout<< *ptr; will.

                          You are right :)

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved