Weird pointer behavior
-
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
-
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
try something like :
GetNextApproach( CApproach** p) { *p = new CApproach; } CApproach* approach = NULL; GetNextApproach(&approach);
Maximilien Lincourt Your Head A Splode - Strong Bad
-
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
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 -
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
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
-
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
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
-
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
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
-
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
it's not a copy, he passing
approach
by reference.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
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
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
-
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
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