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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem when passing by reference

Problem when passing by reference

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 4 Posters 1 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 Offline
    C Offline
    capricious_001
    wrote on last edited by
    #1

    Hey Guys, It must be a very simple mistake I am making however I am receiving a problem when passing a LPSTR value by reference. I have 2 functions. Below are their prototypes:

    bool GetRSLocation_Reg(LPSTR&);
    bool retrieveLogFileLocation(LPSTR&);

    //Call main and do some other stuff

    LPSTR RegLocation

    if(GetRSLocation_Reg(RegLocation)){
    cout << "The output of the RegLocation var is correct here: " << RegLocation << endl;
    if(retrieveLogFileLocation(RegLocation)){
    //Success
    }
    }

    The first function returns in a file path and assigns it to a LPSTR variable by reference. When outputting the referenced value WITHIN the function definition of "GetRSLocation_Reg" to console, it is correct. Before the "retreiveLogFileLocation" function is called, RegLocation also returns the correct value. However within function definition for retreiveLogFileLocation, the contents of the variable RegLocation have changed. It appears to be holding gibberish :s Would anyone know what I am doing wrong that is causing this? Thanks Robbie

    V T H 4 Replies Last reply
    0
    • C capricious_001

      Hey Guys, It must be a very simple mistake I am making however I am receiving a problem when passing a LPSTR value by reference. I have 2 functions. Below are their prototypes:

      bool GetRSLocation_Reg(LPSTR&);
      bool retrieveLogFileLocation(LPSTR&);

      //Call main and do some other stuff

      LPSTR RegLocation

      if(GetRSLocation_Reg(RegLocation)){
      cout << "The output of the RegLocation var is correct here: " << RegLocation << endl;
      if(retrieveLogFileLocation(RegLocation)){
      //Success
      }
      }

      The first function returns in a file path and assigns it to a LPSTR variable by reference. When outputting the referenced value WITHIN the function definition of "GetRSLocation_Reg" to console, it is correct. Before the "retreiveLogFileLocation" function is called, RegLocation also returns the correct value. However within function definition for retreiveLogFileLocation, the contents of the variable RegLocation have changed. It appears to be holding gibberish :s Would anyone know what I am doing wrong that is causing this? Thanks Robbie

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      Probably within the GetRSLocation_Reg function you assign a temporary string (maybe allocated on the stack) to the RegLocation pointer. This string cannot be used after exiting from function, because the stack is reused by other calls. You can try another way, by allocating a space for RegLocation before calling the function:

      bool GetRSLocation_Reg(LPSTR location)
      {
          . . .
          strcpy(location, .....);
          return true;
      }
      
      char RegLocation[256]; // allocate enough space
      if(GetRSLocation_Reg(RegLocation))
      {
          . . .
      }
      

      Alternatively, you can use CString class (in MFC projects) or std::string class from STL.

      C 1 Reply Last reply
      0
      • V Viorel

        Probably within the GetRSLocation_Reg function you assign a temporary string (maybe allocated on the stack) to the RegLocation pointer. This string cannot be used after exiting from function, because the stack is reused by other calls. You can try another way, by allocating a space for RegLocation before calling the function:

        bool GetRSLocation_Reg(LPSTR location)
        {
            . . .
            strcpy(location, .....);
            return true;
        }
        
        char RegLocation[256]; // allocate enough space
        if(GetRSLocation_Reg(RegLocation))
        {
            . . .
        }
        

        Alternatively, you can use CString class (in MFC projects) or std::string class from STL.

        C Offline
        C Offline
        capricious_001
        wrote on last edited by
        #3

        Hey Viorel, Thats exactly what the issue was and I'm quite surprised I didnt see that! I made the changes and everything works perfectly. Thanks! Robbie

        1 Reply Last reply
        0
        • C capricious_001

          Hey Guys, It must be a very simple mistake I am making however I am receiving a problem when passing a LPSTR value by reference. I have 2 functions. Below are their prototypes:

          bool GetRSLocation_Reg(LPSTR&);
          bool retrieveLogFileLocation(LPSTR&);

          //Call main and do some other stuff

          LPSTR RegLocation

          if(GetRSLocation_Reg(RegLocation)){
          cout << "The output of the RegLocation var is correct here: " << RegLocation << endl;
          if(retrieveLogFileLocation(RegLocation)){
          //Success
          }
          }

          The first function returns in a file path and assigns it to a LPSTR variable by reference. When outputting the referenced value WITHIN the function definition of "GetRSLocation_Reg" to console, it is correct. Before the "retreiveLogFileLocation" function is called, RegLocation also returns the correct value. However within function definition for retreiveLogFileLocation, the contents of the variable RegLocation have changed. It appears to be holding gibberish :s Would anyone know what I am doing wrong that is causing this? Thanks Robbie

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          capricious_001 wrote:

          bool GetRSLocation_Reg(LPSTR&); bool retrieveLogFileLocation(LPSTR&);

          this seems to be pointer to pointer problem.. i.e. LPSTR itself is pointer and then you refrencing that also.

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

          1 Reply Last reply
          0
          • C capricious_001

            Hey Guys, It must be a very simple mistake I am making however I am receiving a problem when passing a LPSTR value by reference. I have 2 functions. Below are their prototypes:

            bool GetRSLocation_Reg(LPSTR&);
            bool retrieveLogFileLocation(LPSTR&);

            //Call main and do some other stuff

            LPSTR RegLocation

            if(GetRSLocation_Reg(RegLocation)){
            cout << "The output of the RegLocation var is correct here: " << RegLocation << endl;
            if(retrieveLogFileLocation(RegLocation)){
            //Success
            }
            }

            The first function returns in a file path and assigns it to a LPSTR variable by reference. When outputting the referenced value WITHIN the function definition of "GetRSLocation_Reg" to console, it is correct. Before the "retreiveLogFileLocation" function is called, RegLocation also returns the correct value. However within function definition for retreiveLogFileLocation, the contents of the variable RegLocation have changed. It appears to be holding gibberish :s Would anyone know what I am doing wrong that is causing this? Thanks Robbie

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            capricious_001 wrote:

            bool GetRSLocation_Reg(LPSTR&;); bool retrieveLogFileLocation(LPSTR&;);

            this seems to be pointer to pointer problem.. i.e. LPSTR itself is pointer and then you refrencing that also.

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

            1 Reply Last reply
            0
            • C capricious_001

              Hey Guys, It must be a very simple mistake I am making however I am receiving a problem when passing a LPSTR value by reference. I have 2 functions. Below are their prototypes:

              bool GetRSLocation_Reg(LPSTR&);
              bool retrieveLogFileLocation(LPSTR&);

              //Call main and do some other stuff

              LPSTR RegLocation

              if(GetRSLocation_Reg(RegLocation)){
              cout << "The output of the RegLocation var is correct here: " << RegLocation << endl;
              if(retrieveLogFileLocation(RegLocation)){
              //Success
              }
              }

              The first function returns in a file path and assigns it to a LPSTR variable by reference. When outputting the referenced value WITHIN the function definition of "GetRSLocation_Reg" to console, it is correct. Before the "retreiveLogFileLocation" function is called, RegLocation also returns the correct value. However within function definition for retreiveLogFileLocation, the contents of the variable RegLocation have changed. It appears to be holding gibberish :s Would anyone know what I am doing wrong that is causing this? Thanks Robbie

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #6

              if you want to use LPTSTR use LPTSTR lpt; lpt=LocalAlloc(LPTR,size); and use lpt.... in the endLocalFree(lpt);_**


              **_

              whitesky


              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