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. Function out of scope

Function out of scope

Scheduled Pinned Locked Moved C / C++ / MFC
c++
15 Posts 9 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.
  • N Offline
    N Offline
    Nandu_77b
    wrote on last edited by
    #1

    Hi, In the below code ret value become invalid as it comes out of the funcation scope. can any suggest better idea to solve this. CString ToStr() { CString ret = "test"; return ret; } void fun() { CString val = ToStr(); // it returns nothing (bad pointer) } I have compiled this code in MS VC++2005 under winxp OS. Thanks, Nandu

    _ H C S 4 Replies Last reply
    0
    • N Nandu_77b

      Hi, In the below code ret value become invalid as it comes out of the funcation scope. can any suggest better idea to solve this. CString ToStr() { CString ret = "test"; return ret; } void fun() { CString val = ToStr(); // it returns nothing (bad pointer) } I have compiled this code in MS VC++2005 under winxp OS. Thanks, Nandu

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      void ToStr(CString& val)
      {
      val = "test";
      }

      void fun()
      {
      CString val;
      ToStr(val);
      }

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • N Nandu_77b

        Hi, In the below code ret value become invalid as it comes out of the funcation scope. can any suggest better idea to solve this. CString ToStr() { CString ret = "test"; return ret; } void fun() { CString val = ToStr(); // it returns nothing (bad pointer) } I have compiled this code in MS VC++2005 under winxp OS. Thanks, Nandu

        H Offline
        H Offline
        hrishiS
        wrote on last edited by
        #3

        the problem I could notice is that, ret is a automatic variable , so it is no longer available once it comes out from the function ToStr(). Either you can have it as global variable, or make a variable of pointer/reference of String , inside fun and pass it to ToStr

        ----------------------------- I am a beginner

        C 1 Reply Last reply
        0
        • N Nandu_77b

          Hi, In the below code ret value become invalid as it comes out of the funcation scope. can any suggest better idea to solve this. CString ToStr() { CString ret = "test"; return ret; } void fun() { CString val = ToStr(); // it returns nothing (bad pointer) } I have compiled this code in MS VC++2005 under winxp OS. Thanks, Nandu

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          There's no problem with your code. The problem appears when you return something like this:

          char* ToStr()
          {
          return "test";
          }

          In which case you return the address of a temporary variable. If you return an object, the compiler will make a copy of it and assign it to val correctly. EDIT: changed the return type to char* instead of CString.

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          modified on Tuesday, September 8, 2009 9:54 AM

          S N J 3 Replies Last reply
          0
          • H hrishiS

            the problem I could notice is that, ret is a automatic variable , so it is no longer available once it comes out from the function ToStr(). Either you can have it as global variable, or make a variable of pointer/reference of String , inside fun and pass it to ToStr

            ----------------------------- I am a beginner

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            hrishiS wrote:

            the problem I could notice is that, ret is a automatic variable , so it is no longer available once it comes out from the function ToStr().

            A copy of the object will be returned, so no there's no problem there.

            hrishiS wrote:

            Either you can have it as global variable, or make a variable of pointer/reference of String , inside fun and pass it to ToStr

            1. global variable for that is really ugly (but that's my personal taste) 2) variable of type pointer: not really nice neither because each time you will create the object on the stack and furthermore, you delegate the destruction of the pointer to somewhere else, which potentially introduce memory leaks. 3) return a reference to the object: that's wrong. In this case, the object will be destroyed, no copy will be made and you will return the address of the destroyed object which is wrong. So in conclusion, returning the object as he mentioned is the best option. I could agree with superman in the sense that his proposed solution is more efficient: no temporary object gets created but honestly, it doesn't make a lot of difference (unless your code is in a part of the code which should be highly optimized).

            Cédric Moonen Software developer
            Charting control [v2.0] OpenGL game tutorial in C++

            H 1 Reply Last reply
            0
            • C Cedric Moonen

              hrishiS wrote:

              the problem I could notice is that, ret is a automatic variable , so it is no longer available once it comes out from the function ToStr().

              A copy of the object will be returned, so no there's no problem there.

              hrishiS wrote:

              Either you can have it as global variable, or make a variable of pointer/reference of String , inside fun and pass it to ToStr

              1. global variable for that is really ugly (but that's my personal taste) 2) variable of type pointer: not really nice neither because each time you will create the object on the stack and furthermore, you delegate the destruction of the pointer to somewhere else, which potentially introduce memory leaks. 3) return a reference to the object: that's wrong. In this case, the object will be destroyed, no copy will be made and you will return the address of the destroyed object which is wrong. So in conclusion, returning the object as he mentioned is the best option. I could agree with superman in the sense that his proposed solution is more efficient: no temporary object gets created but honestly, it doesn't make a lot of difference (unless your code is in a part of the code which should be highly optimized).

              Cédric Moonen Software developer
              Charting control [v2.0] OpenGL game tutorial in C++

              H Offline
              H Offline
              hrishiS
              wrote on last edited by
              #6

              thanks for all the points... I can understand How much I have to learn more in deep ...And sorry to the real sender of the question... pls listen/take advice from the the seniors...I am too a learner

              ----------------------------- I am a beginner

              1 Reply Last reply
              0
              • C Cedric Moonen

                There's no problem with your code. The problem appears when you return something like this:

                char* ToStr()
                {
                return "test";
                }

                In which case you return the address of a temporary variable. If you return an object, the compiler will make a copy of it and assign it to val correctly. EDIT: changed the return type to char* instead of CString.

                Cédric Moonen Software developer
                Charting control [v2.0] OpenGL game tutorial in C++

                modified on Tuesday, September 8, 2009 9:54 AM

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                You sure about that Cedric? I think you'll find that "test" is used to construct a CString object that's returned by copy...that's what happened when I compiled and debugged it, anyway :-)

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                C 1 Reply Last reply
                0
                • S Stuart Dootson

                  You sure about that Cedric? I think you'll find that "test" is used to construct a CString object that's returned by copy...that's what happened when I compiled and debugged it, anyway :-)

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  Ooops, sorry,I forgot to change the return type to char* :-O If the return type is CString, then I agree with you, there's no problem neither. Thanks for pointing it out.

                  Cédric Moonen Software developer
                  Charting control [v2.0] OpenGL game tutorial in C++

                  1 Reply Last reply
                  0
                  • N Nandu_77b

                    Hi, In the below code ret value become invalid as it comes out of the funcation scope. can any suggest better idea to solve this. CString ToStr() { CString ret = "test"; return ret; } void fun() { CString val = ToStr(); // it returns nothing (bad pointer) } I have compiled this code in MS VC++2005 under winxp OS. Thanks, Nandu

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    If CString is the standard ATL/MFC string class, then there is nothing wrong with the code you've posted - ret will effectively be copied into val when the return statement is executed. Part of the implied contract of ToStr is that callers will pass the address of a CString object into which the return value can be copied.

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    L 1 Reply Last reply
                    0
                    • C Cedric Moonen

                      There's no problem with your code. The problem appears when you return something like this:

                      char* ToStr()
                      {
                      return "test";
                      }

                      In which case you return the address of a temporary variable. If you return an object, the compiler will make a copy of it and assign it to val correctly. EDIT: changed the return type to char* instead of CString.

                      Cédric Moonen Software developer
                      Charting control [v2.0] OpenGL game tutorial in C++

                      modified on Tuesday, September 8, 2009 9:54 AM

                      N Offline
                      N Offline
                      N a v a n e e t h
                      wrote on last edited by
                      #10

                      I don't find any issues with your code. You are returning by value and not returning any reference to the temporary. So I guess your code is perfectly valid.

                      Cedric Moonen wrote:

                      If you return an object, the compiler will make a copy of it and assign it to val correctly.

                      Even if you return by value, with return value optimization, a copy won't be made. Please correct me if I am wrong here. [edit]Saw your post updated[/edit] :)

                      Navaneeth How to use google | Ask smart questions

                      1 Reply Last reply
                      0
                      • S Stuart Dootson

                        If CString is the standard ATL/MFC string class, then there is nothing wrong with the code you've posted - ret will effectively be copied into val when the return statement is executed. Part of the implied contract of ToStr is that callers will pass the address of a CString object into which the return value can be copied.

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Stuart Dootson wrote:

                        Part of the implied contract of ToStr is that callers will pass the address of a CString object

                        Not exactly - the original post shows:

                        CString ToStr()
                        {
                        CString ret = "test";
                        return ret;
                        }

                        void fun()
                        {
                        CString val = ToStr(); // it returns nothing (bad pointer)
                        }

                        The contract here is that ToStr() will return a CString object, which the caller may store or destroy as he/she wishes. The code as written always returns a valid CString object so I don't see how it can fail. However function fun() does nothing with its CString val so that may be where the original problem occurred.

                        S 1 Reply Last reply
                        0
                        • C Cedric Moonen

                          There's no problem with your code. The problem appears when you return something like this:

                          char* ToStr()
                          {
                          return "test";
                          }

                          In which case you return the address of a temporary variable. If you return an object, the compiler will make a copy of it and assign it to val correctly. EDIT: changed the return type to char* instead of CString.

                          Cédric Moonen Software developer
                          Charting control [v2.0] OpenGL game tutorial in C++

                          modified on Tuesday, September 8, 2009 9:54 AM

                          J Offline
                          J Offline
                          Joe Woodbury
                          wrote on last edited by
                          #12

                          Your example isn't be a problem since "test" is in the data segment and isn't going to change location. I would recommend returning a const char *, but that code is perfectly fine. What isn't fine is the following:

                          char* ToStr()
                          {
                          char buffer[5];
                          lstrcpy(buffer, "test");
                          return buffer;
                          }

                          1 Reply Last reply
                          0
                          • L Lost User

                            Stuart Dootson wrote:

                            Part of the implied contract of ToStr is that callers will pass the address of a CString object

                            Not exactly - the original post shows:

                            CString ToStr()
                            {
                            CString ret = "test";
                            return ret;
                            }

                            void fun()
                            {
                            CString val = ToStr(); // it returns nothing (bad pointer)
                            }

                            The contract here is that ToStr() will return a CString object, which the caller may store or destroy as he/she wishes. The code as written always returns a valid CString object so I don't see how it can fail. However function fun() does nothing with its CString val so that may be where the original problem occurred.

                            S Offline
                            S Offline
                            Stuart Dootson
                            wrote on last edited by
                            #13

                            Note the use of the word 'implied' :-) An object (of struct/class type) gets returned from a function in C/C++ by the caller making space for it and the return statement in the function copying the actual value returned (ret in this case) into this space, constructing a temporary, unnamed object. This temporary object is then used in the callers context (in this case, it gets assigned to val). A compiler is permitted to optimise away this temporary object when suitable, by giving the function a reference to the final destination (in this case val) rather than the temporary object.

                            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                            L M 2 Replies Last reply
                            0
                            • S Stuart Dootson

                              Note the use of the word 'implied' :-) An object (of struct/class type) gets returned from a function in C/C++ by the caller making space for it and the return statement in the function copying the actual value returned (ret in this case) into this space, constructing a temporary, unnamed object. This temporary object is then used in the callers context (in this case, it gets assigned to val). A compiler is permitted to optimise away this temporary object when suitable, by giving the function a reference to the final destination (in this case val) rather than the temporary object.

                              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Good point, I had not considered the full implications of optimisation.

                              1 Reply Last reply
                              0
                              • S Stuart Dootson

                                Note the use of the word 'implied' :-) An object (of struct/class type) gets returned from a function in C/C++ by the caller making space for it and the return statement in the function copying the actual value returned (ret in this case) into this space, constructing a temporary, unnamed object. This temporary object is then used in the callers context (in this case, it gets assigned to val). A compiler is permitted to optimise away this temporary object when suitable, by giving the function a reference to the final destination (in this case val) rather than the temporary object.

                                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                M Offline
                                M Offline
                                Moak
                                wrote on last edited by
                                #15

                                <Slashdot style comment> Vote parent up! :)

                                Webchat in Europe :java: (only 4K)

                                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