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. ATL / WTL / STL
  4. Passing CString

Passing CString

Scheduled Pinned Locked Moved ATL / WTL / STL
question
8 Posts 5 Posters 3 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.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    If method A calls B and B is to return or provide a CString to A, what is the better way to do this? The declaration for B can look like: CString B(); Or Void B( CString &out_string ); I am thinking the latter should be preferred. The former is simpler but it seems to expose its internal data to the caller.

    Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

    L P M 4 Replies Last reply
    0
    • B bkelly13

      If method A calls B and B is to return or provide a CString to A, what is the better way to do this? The declaration for B can look like: CString B(); Or Void B( CString &out_string ); I am thinking the latter should be preferred. The former is simpler but it seems to expose its internal data to the caller.

      Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

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

      Option 1 is the generally accepted method style, unless B needs to return a status value like:

      BOOL B( CString &out_string );

      Use the best guess

      1 Reply Last reply
      0
      • B bkelly13

        If method A calls B and B is to return or provide a CString to A, what is the better way to do this? The declaration for B can look like: CString B(); Or Void B( CString &out_string ); I am thinking the latter should be preferred. The former is simpler but it seems to expose its internal data to the caller.

        Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

        P Offline
        P Offline
        pasztorpisti
        wrote on last edited by
        #3

        Any decent compiler with return value optimization will compile about the same machine code from the above method declarations so I would use CString B(); because it looks nicer. The second version seems much like "human optimized" code but its not that terrible. From this example what came to my mind is the usual CString ToString(); method. Its easier to use the first declaration because the second always requires you to declare variables on the stack before the call while the first one allows you to write nicer code, for example you can immediately pass the return value of B() to another function call that receives a string. Another thing you need to know here: Even if there is no return value optimization in your compiler (Visual C++ has it) CString is a reference counted string that means: It is just a "pointer" to the string data so copying it copies only a pointer and increases then decreases a refcount of the string data when the returned temp refcount-pointer object (CString) is deleted.

        B 1 Reply Last reply
        0
        • P pasztorpisti

          Any decent compiler with return value optimization will compile about the same machine code from the above method declarations so I would use CString B(); because it looks nicer. The second version seems much like "human optimized" code but its not that terrible. From this example what came to my mind is the usual CString ToString(); method. Its easier to use the first declaration because the second always requires you to declare variables on the stack before the call while the first one allows you to write nicer code, for example you can immediately pass the return value of B() to another function call that receives a string. Another thing you need to know here: Even if there is no return value optimization in your compiler (Visual C++ has it) CString is a reference counted string that means: It is just a "pointer" to the string data so copying it copies only a pointer and increases then decreases a refcount of the string data when the returned temp refcount-pointer object (CString) is deleted.

          B Offline
          B Offline
          bkelly13
          wrote on last edited by
          #4

          Am I correct in thinking that "return value optimization" self descriptive rather than having a special meaning? In the first call B, more of a function style, is written as: CString B( ){ return internal_string } While the second, more of a procedure style, would look like: void B( CSrting &some_string ); { some_string.empty(); some_string = internal_string; } That presumes that some_string = internal_string is a copy operation and not a reference operation. Is that presumption true or false? The first hands out an address within B's space while the second does not. That also means that the caller would have to copy it before doing anything that would change it. I know we can adorn the return value with "const", but still. Is that logic flawed?

          Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

          P H 2 Replies Last reply
          0
          • B bkelly13

            Am I correct in thinking that "return value optimization" self descriptive rather than having a special meaning? In the first call B, more of a function style, is written as: CString B( ){ return internal_string } While the second, more of a procedure style, would look like: void B( CSrting &some_string ); { some_string.empty(); some_string = internal_string; } That presumes that some_string = internal_string is a copy operation and not a reference operation. Is that presumption true or false? The first hands out an address within B's space while the second does not. That also means that the caller would have to copy it before doing anything that would change it. I know we can adorn the return value with "const", but still. Is that logic flawed?

            Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

            P Offline
            P Offline
            pasztorpisti
            wrote on last edited by
            #5

            If you are interested about how optimizations work please use google. You can never expect a compiler to optimize your code at a particular code location but please don't write ugly code just to try to optimize out things prematurely. Whether to make a piece of code manually-optimized and ugly depends on a few factors: The size of the data structure you want to return, the frequency with which the function is being called,... Keep the code pretty and easy to read and optimize/uglify the critical often-executed parts. Optimization takes place periodically during development. While optimizing you find out what part of the code takes most of the execution time and you cut it off here and there. Often these codepieces are found at the most unexpected places and the biggest performance degradations happen due to algorithmic problems.

            1 Reply Last reply
            0
            • B bkelly13

              If method A calls B and B is to return or provide a CString to A, what is the better way to do this? The declaration for B can look like: CString B(); Or Void B( CString &out_string ); I am thinking the latter should be preferred. The former is simpler but it seems to expose its internal data to the caller.

              Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

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

              Option 2 can be preferred for object return. Option 1 is usually used to return primitive types.

              1 Reply Last reply
              0
              • B bkelly13

                If method A calls B and B is to return or provide a CString to A, what is the better way to do this? The declaration for B can look like: CString B(); Or Void B( CString &out_string ); I am thinking the latter should be preferred. The former is simpler but it seems to expose its internal data to the caller.

                Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                M Offline
                M Offline
                Manoj Kumar Rai
                wrote on last edited by
                #7

                It depends on few things. Most importantly what bussiness logic is there in B. For example suppose B function is responsible to read data from a very large file and you want to return that from B. There are changes that file open error may occure then: int B(CString &out_string) will be best option because of two reasons. 1. The error code can be returned as return parameter 2. You dont need to accumulate the content of file in a local variable which gets added and deleted on stack while transferring that as return value.

                Manoj Never Gives up

                1 Reply Last reply
                0
                • B bkelly13

                  Am I correct in thinking that "return value optimization" self descriptive rather than having a special meaning? In the first call B, more of a function style, is written as: CString B( ){ return internal_string } While the second, more of a procedure style, would look like: void B( CSrting &some_string ); { some_string.empty(); some_string = internal_string; } That presumes that some_string = internal_string is a copy operation and not a reference operation. Is that presumption true or false? The first hands out an address within B's space while the second does not. That also means that the caller would have to copy it before doing anything that would change it. I know we can adorn the return value with "const", but still. Is that logic flawed?

                  Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                  H Offline
                  H Offline
                  H Brydon
                  wrote on last edited by
                  #8

                  Your analysis is a bit flawed. First of all, you don't need to empty the string before populating it:

                  bkelly13 wrote:

                  void B( CString &some_string ); // (spelling error fixed) { some_string.empty(); // this line is unnecessary some_string = internal_string; }

                  There are several things that happen in this (fixed) code that don't happen in the 'first call' version. When calling the API, there is more setup on the part of the caller. A CString variable needs to exist already, and API setup makes sure that it is either empty or ref count is zero (and writable). Assuming that internal_string is another CString, no copying takes place. The some_string object's header info is updated to duplicate the same info in internal_string, and the ref count is incremented. If internal_string is 1MB in size, only a pointer and counter are changed (ie. no copying). Again, if internal_string is a CString, a similar process also happens in your 'first call' code (ie. no copying). If internal_string is a (const?) char* or (const?) char[] or something else that looks like one of those, then a copy needs to take place for both versions of the code.

                  Windows 8 is the resurrected version of Microsoft Bob. The only thing missing is the Fisher-Price logo. - Harvey

                  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