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
Forum Update on Monday, May 27th 2025

Ambiguity when non constant pointer is pointing to constant variable

Scheduled Pinned Locked Moved Unsolved C++ Gurus
12 Posts 7 Posters 1.2k 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 21 Apr 2022, 09:39 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!

    J J 2 Replies Last reply 21 Apr 2022, 10:37
    0
    • V Vinoth Rajendran4
      21 Apr 2022, 09:39

      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 Offline
      J Offline
      JonB
      wrote on 21 Apr 2022, 10:37 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 21 Apr 2022, 11:24
      0
      • V Vinoth Rajendran4
        21 Apr 2022, 09:39

        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 Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 21 Apr 2022, 10:51 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 21 Apr 2022, 12:15
        1
        • J JonB
          21 Apr 2022, 10:37

          @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 21 Apr 2022, 11:24 last edited by
          #4

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

          K J 2 Replies Last reply 21 Apr 2022, 11:32
          0
          • V Vinoth Rajendran4
            21 Apr 2022, 11:24

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

            K Offline
            K Offline
            KroMignon
            wrote on 21 Apr 2022, 11:32 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)

            F 1 Reply Last reply 22 Apr 2022, 22:59
            0
            • J J.Hilk
              21 Apr 2022, 10:51

              @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 21 Apr 2022, 12:15 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<> ?

              K J 2 Replies Last reply 21 Apr 2022, 12:18
              0
              • V Vinoth Rajendran4
                21 Apr 2022, 12:15

                @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<> ?

                K Offline
                K Offline
                KroMignon
                wrote on 21 Apr 2022, 12:18 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
                  21 Apr 2022, 11:24

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 21 Apr 2022, 12:18 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
                    21 Apr 2022, 12:15

                    @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<> ?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 21 Apr 2022, 12:18 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
                    • K KroMignon
                      21 Apr 2022, 11:32

                      @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;
                      
                      F Offline
                      F Offline
                      fcarney
                      wrote on 22 Apr 2022, 22:59 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.

                      K 1 Reply Last reply 24 Apr 2022, 20:16
                      2
                      • K Offline
                        K Offline
                        Kent-Dorfman
                        wrote on 23 Apr 2022, 02:31 last edited by Kent-Dorfman
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • F fcarney
                          22 Apr 2022, 22:59

                          @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.

                          K Offline
                          K Offline
                          KroMignon
                          wrote on 24 Apr 2022, 20:16 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

                          5/12

                          21 Apr 2022, 11:32

                          • Login

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