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. Weird pointer behavior

Weird pointer behavior

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomdebugging
9 Posts 6 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.
  • K Offline
    K Offline
    K Shaffer
    wrote on last edited by
    #1

    I know there is a reasonable explination behind what is going on here, but I can't figure it out. I create a pointer, pass it into a method and when the method returns, the value of the pointer is NULL. If I trace the method call, there is a valid pointer all the way until GetNextApproach returns. Any ideas? CApproach* approach = NULL; GetNextApproach(approach); // approach is NULL here -Kevin Shaffer kshaff03@yahoo.com

    R M M K C 5 Replies Last reply
    0
    • K K Shaffer

      I know there is a reasonable explination behind what is going on here, but I can't figure it out. I create a pointer, pass it into a method and when the method returns, the value of the pointer is NULL. If I trace the method call, there is a valid pointer all the way until GetNextApproach returns. Any ideas? CApproach* approach = NULL; GetNextApproach(approach); // approach is NULL here -Kevin Shaffer kshaff03@yahoo.com

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      try something like : GetNextApproach( CApproach** p) { *p = new CApproach; } CApproach* approach = NULL; GetNextApproach(&approach);


      Maximilien Lincourt Your Head A Splode - Strong Bad

      1 Reply Last reply
      0
      • K K Shaffer

        I know there is a reasonable explination behind what is going on here, but I can't figure it out. I create a pointer, pass it into a method and when the method returns, the value of the pointer is NULL. If I trace the method call, there is a valid pointer all the way until GetNextApproach returns. Any ideas? CApproach* approach = NULL; GetNextApproach(approach); // approach is NULL here -Kevin Shaffer kshaff03@yahoo.com

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        That's how function parameters work. Any changes that GetNextApproach() makes to its parameter are not reflected in the caller. Make the parameter a reference (CApproach*&) to have the caller see the change. --Mike-- LINKS~! Ericahist updated! | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD

        1 Reply Last reply
        0
        • K K Shaffer

          I know there is a reasonable explination behind what is going on here, but I can't figure it out. I create a pointer, pass it into a method and when the method returns, the value of the pointer is NULL. If I trace the method call, there is a valid pointer all the way until GetNextApproach returns. Any ideas? CApproach* approach = NULL; GetNextApproach(approach); // approach is NULL here -Kevin Shaffer kshaff03@yahoo.com

          R Offline
          R Offline
          rocky_pulley
          wrote on last edited by
          #4

          Why should it not be null? That's what you set it at. Passing the address of the pointer can make it modify it but just passing the pointer itself won't modify it. Let me explain. See the code and the two functions below. Since you are not giving the address of the pointer to the first one, the pointer is not changing from null, it is only valid inside of your function. char *sz = NULL; func1(sz); //sz is still null here. func2(&sz); //ok now sz is changed. func1(char *sz) { sz = new char[1024]; strcpy(sz, "blah"); } func2(char **sz) { *sz = new char[1024]; strcpy(*sz, "blah"); } This is probably what you want your code to do: CApproach approach; //notice, not a pointer. GetNextApproach(&approach); -- Rocky Dean Pulley

          K 1 Reply Last reply
          0
          • K K Shaffer

            I know there is a reasonable explination behind what is going on here, but I can't figure it out. I create a pointer, pass it into a method and when the method returns, the value of the pointer is NULL. If I trace the method call, there is a valid pointer all the way until GetNextApproach returns. Any ideas? CApproach* approach = NULL; GetNextApproach(approach); // approach is NULL here -Kevin Shaffer kshaff03@yahoo.com

            K Offline
            K Offline
            K Shaffer
            wrote on last edited by
            #5

            Ok, I think I am understanding now. This is valid because the function is changing the contents of where the pointer I am passing in points to, not the actual value of the pointer itself. char g_code[APP_AIRPORT_CODE_SIZE + 1]; GetCode(g_code); GetCode(char* code) { strcpy(code, "Code"); } However this does not work as I expected because I am trying to change the actual value of the pointer g_code: char* g_code; GetCode(g_code) { g_code = GetPointerToArraysomeOtherPlace(); } Please let me know if I am still in la la land. Thank you for your answers. Kevin Shaffer kshaff03@msn.com

            1 Reply Last reply
            0
            • R rocky_pulley

              Why should it not be null? That's what you set it at. Passing the address of the pointer can make it modify it but just passing the pointer itself won't modify it. Let me explain. See the code and the two functions below. Since you are not giving the address of the pointer to the first one, the pointer is not changing from null, it is only valid inside of your function. char *sz = NULL; func1(sz); //sz is still null here. func2(&sz); //ok now sz is changed. func1(char *sz) { sz = new char[1024]; strcpy(sz, "blah"); } func2(char **sz) { *sz = new char[1024]; strcpy(*sz, "blah"); } This is probably what you want your code to do: CApproach approach; //notice, not a pointer. GetNextApproach(&approach); -- Rocky Dean Pulley

              K Offline
              K Offline
              K Shaffer
              wrote on last edited by
              #6

              rocky_pulley wrote: This is probably what you want your code to do: CApproach approach; //notice, not a pointer. GetNextApproach(&approach); Yes, except I don't want to make a copy, I just want to reference an object that is already created. I also want to know, if I did the following, this would create a memory leak, right? CApproach approach; GetNextApproach(&approach); GetNextApproach(CApproach* approach) { approach = GetAddressOfApproachSomePlaceElse(); } Kevin Shaffer kshaff03@msn.com

              M T 2 Replies Last reply
              0
              • K K Shaffer

                rocky_pulley wrote: This is probably what you want your code to do: CApproach approach; //notice, not a pointer. GetNextApproach(&approach); Yes, except I don't want to make a copy, I just want to reference an object that is already created. I also want to know, if I did the following, this would create a memory leak, right? CApproach approach; GetNextApproach(&approach); GetNextApproach(CApproach* approach) { approach = GetAddressOfApproachSomePlaceElse(); } Kevin Shaffer kshaff03@msn.com

                M Offline
                M Offline
                Maximilien
                wrote on last edited by
                #7

                it's not a copy, he passing approach by reference.


                Maximilien Lincourt Your Head A Splode - Strong Bad

                1 Reply Last reply
                0
                • K K Shaffer

                  rocky_pulley wrote: This is probably what you want your code to do: CApproach approach; //notice, not a pointer. GetNextApproach(&approach); Yes, except I don't want to make a copy, I just want to reference an object that is already created. I also want to know, if I did the following, this would create a memory leak, right? CApproach approach; GetNextApproach(&approach); GetNextApproach(CApproach* approach) { approach = GetAddressOfApproachSomePlaceElse(); } Kevin Shaffer kshaff03@msn.com

                  T Offline
                  T Offline
                  TFrancis
                  wrote on last edited by
                  #8

                  If, GetAddressOfApproachSomePlaceElse() calls new, then yes, this would be a memory leak. If in your function you want to change what object a pointer points to (not the object itself, but what object it points to), then one technique is to pass it a pointer to a pointer. CApproach* pApproach = NULL; GetNextApproach(&pApproach) void GetNextApproach(CApproach** ppApproach) { *ppApproach = GetAddressOfApproachSomePlaceElse(); } If you simply pass it a pointer, then it makes a copy of the pointer (not a copy of the object, but a copy of the pointer to the object). Any changes you make to the pointer (not the object, but the pointer to the object) will not be kept when the function returns, they are thrown away with the copy of the pointer. Hope this helps, Tim

                  1 Reply Last reply
                  0
                  • K K Shaffer

                    I know there is a reasonable explination behind what is going on here, but I can't figure it out. I create a pointer, pass it into a method and when the method returns, the value of the pointer is NULL. If I trace the method call, there is a valid pointer all the way until GetNextApproach returns. Any ideas? CApproach* approach = NULL; GetNextApproach(approach); // approach is NULL here -Kevin Shaffer kshaff03@yahoo.com

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    The correct approach would be: CApproach* approach = GetNextApproach(); The other responses solve the problem ( not passing by reference ), but they ignore the fact that you're passing null in, all you want is for the value to be returned. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                    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