Return type from a sub-function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anandkumar
    New Member
    • May 2010
    • 5

    Return type from a sub-function

    Could you please let me know whether the following return types from a sub function are valid?
    • Structure (struct <structname> subfunction_nam e(<data type>))
    • Pointer (<data type> * subfunction_nam e(<data type>))


    If the above types are invalid, then which do you suggest is a better way to return data as a substitute to the methods mentioned above?

    Thank you :-)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Both a structure and a point are valid return values.

    However, I would not return any value from a function unless I had a requirement to use the function as an RVAL.

    Then if that were the case, I would return a handle rather than a struct or a pointer. You might read: http://bytes.com/topic/c/insights/65...-smart-pointer.

    If the function did not need to be an RVAL, then I would use the return for passing back completion codes. Note that if you use a handle, you won't need an argument to pass it back since handles act as references but behave like pointers. That way you can pass a handle to a function and that will allow the function access to the object managed by the handle.

    Comment

    • anandkumar
      New Member
      • May 2010
      • 5

      #3
      Originally posted by weaknessforcats
      Both a structure and a point are valid return values.

      However, I would not return any value from a function unless I had a requirement to use the function as an RVAL.

      Then if that were the case, I would return a handle rather than a struct or a pointer. You might read: http://bytes.com/topic/c/insights/65...-smart-pointer.

      If the function did not need to be an RVAL, then I would use the return for passing back completion codes. Note that if you use a handle, you won't need an argument to pass it back since handles act as references but behave like pointers. That way you can pass a handle to a function and that will allow the function access to the object managed by the handle.
      Hi 'weaknessforcat s':-)

      Thanks for providing me with answers and also for providing me with an insight to 'handles'. I found it pretty useful :-)

      Regards,
      Anand

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I'm not sure if this was a C or C++ question but in C (unlike C++) returning a structure from a function would normally be considered bad practice. Because of the way C handles structures there are some thread safety issues and other struct copying issues.

        It is possible that at least some of these are legacy and don't occur any more but habits dies hard in the C community.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          The same is true in C++ when you return a struct. That is, a copy is made. In C++ you do have the ability to write a copy constructor to make this copy rather than be subject to arbitrary copy rules but this is a function call and making that copy will itself take even more time.

          You should avoid returning structs (or classes) in C++.

          Comment

          • mayankarpit
            New Member
            • Jun 2010
            • 13

            #6
            Make a habit of returning return codes from API's instead of returning pointers. pointers which needs to returned should be passed as argument to API's. This will also help in making APIs thread safe.

            Comment

            • anandkumar
              New Member
              • May 2010
              • 5

              #7
              Originally posted by mayankarpit
              Make a habit of returning return codes from API's instead of returning pointers. pointers which needs to returned should be passed as argument to API's. This will also help in making APIs thread safe.
              sure will keep that in mind mayank :-) thanks for the suggestion :-)

              Comment

              Working...