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. Other Discussions
  3. The Weird and The Wonderful
  4. Fun with pointers in C++

Fun with pointers in C++

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++question
33 Posts 16 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.
  • C C3D1

    Sorry, that's not my code, an i cant change it. I just saw it in some code and where surprised what's that :D Returning references would make it much harder to read:

    GetA() = GetB()

    looks like What the hell? Assignment to a Function? :omg: :wtf:

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

    Learn to read code. Assignment to a function would be

    GetA = GetB()

    which would mean GetB() returns a function pointer rather than an int* and GetA is function pointer rather than a function. Learn to read code.

    #SupportHeForShe

    If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

    1 Reply Last reply
    0
    • A Al Chak

      Your code will be correct if both GetA and GetB return pointer on GLOBAL or STATIC variable same type ofcorse. It is possible to code on C++ without C-knowledge, but not to programm :cool:

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

      It would also be correct within the context of the code for a class where the functions are returning pointers to data members.

      #SupportHeForShe

      If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

      1 Reply Last reply
      0
      • J James Curran

        My only problem with that is that GetA and GetB are being called twice each. There is, of course, no guarantee that the second call to either will return the same value as the first. Further, there is not guarantee that either isn't an expensive operation. I would probably go for the much clearer: A* pa = GetA(); B* pb = GetB(); if (pa != NULL && pb!= NULL) *pa = *pb; Unfortunately, this method would always call GetB once, while the original would never call GetB if the first call to GetA returned NULL, so determining which is more efficient depends of how expensive the call to GetB is, and the likelihood than GetA returns null. Which would give us this: A* pa = GetA(); if (pa != NULL) { B* pb = GetB(); if (pb != NULL) *pa = *pb; } Which, despite being the most keystrokes, would be the best method in terms of speed efficiency, memory efficiency (fewest assembly instructions), and code clarity. In other words, just freakin' learn to type.

        Truth, James

        T Offline
        T Offline
        TheGreatAndPowerfulOz
        wrote on last edited by
        #18

        what about:

        int* a;
        int* b;
        if ((a = GetA()) && (b = GetB()))
        {
        *a = *b;
        }

        #SupportHeForShe

        If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

        J 1 Reply Last reply
        0
        • T TheGreatAndPowerfulOz

          what about:

          int* a;
          int* b;
          if ((a = GetA()) && (b = GetB()))
          {
          *a = *b;
          }

          #SupportHeForShe

          If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

          J Offline
          J Offline
          James Curran
          wrote on last edited by
          #19

          That does work, but

          if (a = GetA()) ...

          is too easy to mistake for

          if (a == GetA()) ...

          Which is why I never leave the comparison implied, so we get:

          if ((a = GetA()) != NULL && (b = GetB()) != NULL)

          which is rather unwieldy. And for what purpose? The longer version I posted will produce the exact same object code.

          Truth, James

          T 1 Reply Last reply
          0
          • J James Curran

            That does work, but

            if (a = GetA()) ...

            is too easy to mistake for

            if (a == GetA()) ...

            Which is why I never leave the comparison implied, so we get:

            if ((a = GetA()) != NULL && (b = GetB()) != NULL)

            which is rather unwieldy. And for what purpose? The longer version I posted will produce the exact same object code.

            Truth, James

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

            James Curran wrote:

            The longer version I posted will produce the exact same object code.

            Will it? The version you and I just discussed has the advantage of short-circuiting, where the first version you posted does not. Which is a "limitation" you pointed-out.

            #SupportHeForShe

            If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

            J 1 Reply Last reply
            0
            • T TheGreatAndPowerfulOz

              James Curran wrote:

              The longer version I posted will produce the exact same object code.

              Will it? The version you and I just discussed has the advantage of short-circuiting, where the first version you posted does not. Which is a "limitation" you pointed-out.

              #SupportHeForShe

              If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

              J Offline
              J Offline
              James Curran
              wrote on last edited by
              #21

              I was referring to the second version I posted (i.e., "the longer version", the one with nested if()s). And that does produce identical object code. From VisualStudio 2013, Release build: Mine:

              ; 21 : void Method2()
              ; 22 : {
              ; 23 : A* pa = GetA();

              00023 e8 00 00 00 00 call ?GetA@@YAPAHXZ ; GetA
              00028 8b f0 mov esi, eax

              ; 24 : if (pa != NULL)

              0002a 85 f6 test esi, esi
              0002c 74 0d je SHORT $LN6@wmain

              ; 25 : {
              ; 26 : B* pb = GetB();

              0002e e8 00 00 00 00 call ?GetB@@YAPAHXZ ; GetB

              ; 27 : if (pb != NULL)

              00033 85 c0 test eax, eax
              00035 74 04 je SHORT $LN6@wmain

              ; 28 : *pa = *pb;

              00037 8b 08 mov ecx, DWORD PTR [eax]
              00039 89 0e mov DWORD PTR [esi], ecx
              $LN6@wmain:

              ; 29 : }
              ; 30 : }

              and yours:

              ; 32 : void Method3()
              ; 33 : {
              ; 34 : A* a;
              ; 35 : B* b;
              ; 36 : if ((a = GetA()) && (b = GetB()))

              0003b e8 00 00 00 00 call ?GetA@@YAPAHXZ ; GetA
              00040 8b f0 mov esi, eax
              00042 85 f6 test esi, esi
              00044 74 0d je SHORT $LN13@wmain
              00046 e8 00 00 00 00 call ?GetB@@YAPAHXZ ; GetB
              0004b 85 c0 test eax, eax
              0004d 74 04 je SHORT $LN13@wmain

              ; 37 : {
              ; 38 : *a = *b;

              0004f 8b 08 mov ecx, DWORD PTR [eax]
              00051 89 0e mov DWORD PTR [esi], ecx
              $LN13@wmain:

              That's with all standard "Release mode" optimizations on, except "Whole Program Optimization" (to prevent it from inlining GetA & GetB)

              Truth, James

              T 1 Reply Last reply
              0
              • J James Curran

                I was referring to the second version I posted (i.e., "the longer version", the one with nested if()s). And that does produce identical object code. From VisualStudio 2013, Release build: Mine:

                ; 21 : void Method2()
                ; 22 : {
                ; 23 : A* pa = GetA();

                00023 e8 00 00 00 00 call ?GetA@@YAPAHXZ ; GetA
                00028 8b f0 mov esi, eax

                ; 24 : if (pa != NULL)

                0002a 85 f6 test esi, esi
                0002c 74 0d je SHORT $LN6@wmain

                ; 25 : {
                ; 26 : B* pb = GetB();

                0002e e8 00 00 00 00 call ?GetB@@YAPAHXZ ; GetB

                ; 27 : if (pb != NULL)

                00033 85 c0 test eax, eax
                00035 74 04 je SHORT $LN6@wmain

                ; 28 : *pa = *pb;

                00037 8b 08 mov ecx, DWORD PTR [eax]
                00039 89 0e mov DWORD PTR [esi], ecx
                $LN6@wmain:

                ; 29 : }
                ; 30 : }

                and yours:

                ; 32 : void Method3()
                ; 33 : {
                ; 34 : A* a;
                ; 35 : B* b;
                ; 36 : if ((a = GetA()) && (b = GetB()))

                0003b e8 00 00 00 00 call ?GetA@@YAPAHXZ ; GetA
                00040 8b f0 mov esi, eax
                00042 85 f6 test esi, esi
                00044 74 0d je SHORT $LN13@wmain
                00046 e8 00 00 00 00 call ?GetB@@YAPAHXZ ; GetB
                0004b 85 c0 test eax, eax
                0004d 74 04 je SHORT $LN13@wmain

                ; 37 : {
                ; 38 : *a = *b;

                0004f 8b 08 mov ecx, DWORD PTR [eax]
                00051 89 0e mov DWORD PTR [esi], ecx
                $LN13@wmain:

                That's with all standard "Release mode" optimizations on, except "Whole Program Optimization" (to prevent it from inlining GetA & GetB)

                Truth, James

                T Offline
                T Offline
                TheGreatAndPowerfulOz
                wrote on last edited by
                #22

                Interesting. Thanks.

                #SupportHeForShe

                If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                1 Reply Last reply
                0
                • D Daniel Pfeffer

                  C3D1 wrote:

                  Returning references would make it much harder to read

                  We'll just have to agree to disagree on that. IMO, this is a quite useful C++ paradigm.

                  If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                  K Offline
                  K Offline
                  KP Lee
                  wrote on last edited by
                  #23

                  Daniel Pfeffer wrote:

                  IMO, this is a quite useful C++ paradigm.

                  ...and that makes me even happier to barely being able to read C++ :laugh:

                  D 1 Reply Last reply
                  0
                  • K KP Lee

                    Daniel Pfeffer wrote:

                    IMO, this is a quite useful C++ paradigm.

                    ...and that makes me even happier to barely being able to read C++ :laugh:

                    D Offline
                    D Offline
                    Daniel Pfeffer
                    wrote on last edited by
                    #24

                    De gustibus non est disputandum :)

                    If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                    K B 2 Replies Last reply
                    0
                    • D Daniel Pfeffer

                      De gustibus non est disputandum :)

                      If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                      K Offline
                      K Offline
                      KP Lee
                      wrote on last edited by
                      #25

                      That's what I like about this forum. You go in thinking programming and you get a bit of Latin training. Hope I remember what the phrase means next time I see it.

                      D 1 Reply Last reply
                      0
                      • K KP Lee

                        That's what I like about this forum. You go in thinking programming and you get a bit of Latin training. Hope I remember what the phrase means next time I see it.

                        D Offline
                        D Offline
                        Daniel Pfeffer
                        wrote on last edited by
                        #26

                        Me too. There are plenty of sites around that merely answer programming questions, but only a few with the range of highly intelligent, humorous, and opinionated contributors that you find here. Amazingly, the amount of sniping and backbiting is kept to a minimum! Long may it last!

                        If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                        G K 2 Replies Last reply
                        0
                        • D Daniel Pfeffer

                          Me too. There are plenty of sites around that merely answer programming questions, but only a few with the range of highly intelligent, humorous, and opinionated contributors that you find here. Amazingly, the amount of sniping and backbiting is kept to a minimum! Long may it last!

                          If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                          G Offline
                          G Offline
                          grralph1
                          wrote on last edited by
                          #27

                          Beautifully expressed and I agree totally.

                          "Rock journalism is people who can't write interviewing people who can't talk for people who can't read." Frank Zappa 1980

                          1 Reply Last reply
                          0
                          • D Daniel Pfeffer

                            Me too. There are plenty of sites around that merely answer programming questions, but only a few with the range of highly intelligent, humorous, and opinionated contributors that you find here. Amazingly, the amount of sniping and backbiting is kept to a minimum! Long may it last!

                            If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                            K Offline
                            K Offline
                            KP Lee
                            wrote on last edited by
                            #28

                            Daniel Pfeffer wrote:

                            Amazingly, the amount of sniping and backbiting is kept to a minimum!

                            Hear Hear (Or is that Here Here? :laugh: ) Sometimes I find myself biting my tongue, but not on this thread. (Yet, and this thread is starting to grow a beard)

                            1 Reply Last reply
                            0
                            • M Mario Vernari

                              In two hours you can have a great lunch, jogging, some fun with your partner, some nice song listening/playing and also a shower. All that cannot be done because the language is cryptic: that's why I hate C/C++! Good post, though...

                              K Offline
                              K Offline
                              KP Lee
                              wrote on last edited by
                              #29

                              Mario Vernari wrote:

                              that's why I hate C/C++!

                              Yea, I kind of agree with that sentiment, however, I have yet to run across the language that will supply me with a great lunch, but I do admit to sometimes having fun with it. (Does that mean I'm cheating on my partner?)

                              1 Reply Last reply
                              0
                              • M Mario Vernari

                                In two hours you can have a great lunch, jogging, some fun with your partner, some nice song listening/playing and also a shower. All that cannot be done because the language is cryptic: that's why I hate C/C++! Good post, though...

                                K Offline
                                K Offline
                                KP Lee
                                wrote on last edited by
                                #30

                                Mario Vernari wrote:

                                that's why I hate C/C++!

                                Yea, I kind of agree with that sentiment, however, I have yet to run across the language that will supply me with a great lunch, but I do admit to sometimes having fun with it. (Not C++ and does that mean I'm cheating on my partner?)

                                1 Reply Last reply
                                0
                                • D Daniel Pfeffer

                                  De gustibus non est disputandum :)

                                  If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                                  B Offline
                                  B Offline
                                  Bernhard Hiller
                                  wrote on last edited by
                                  #31

                                  Daniel Pfeffer wrote:

                                  De gustibus non est disputandum :)

                                  I.e. C++ is the Durian (stinkfruit) in Software Development.

                                  1 Reply Last reply
                                  0
                                  • D Daniel Pfeffer

                                    C3D1 wrote:

                                    Returning references would make it much harder to read

                                    We'll just have to agree to disagree on that. IMO, this is a quite useful C++ paradigm.

                                    If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                                    R Offline
                                    R Offline
                                    Rob Grainger
                                    wrote on last edited by
                                    #32

                                    I agree, but I definitely would not name such a function "GetA".

                                    "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                    1 Reply Last reply
                                    0
                                    • C C3D1

                                      Sorry, that's not my code, an i cant change it. I just saw it in some code and where surprised what's that :D Returning references would make it much harder to read:

                                      GetA() = GetB()

                                      looks like What the hell? Assignment to a Function? :omg: :wtf:

                                      L Offline
                                      L Offline
                                      Luiz Monad
                                      wrote on last edited by
                                      #33

                                      You can't read C++ code without knowing the types. Who knows if equals was overridden, everything could happen. Even then you should read, GetA() return something, then operator = is called on this something.

                                      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