CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    std::string as function parameter

    Hello,

    I have planty of functions which have std::string as parameter
    the string value isn't change inside those functions (I send them as a const)

    My question is, should I pass the std::string by value like it was int/double etc
    or by reference like I do with structs?

    I mean

    const std::string myString

    func1(myString)

    should I define func1 like this:
    std::string func1(const std::string& myStr)

    or

    std::string func1(const std::string myStr)


    is std::string is a big structure needs using reference?

    thanks a lot
    avi

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    Avi,

    You should probably send them in as constant references. This avoids copying the input string into the subroutine and results in improved run-time performance.

    Sincerely, Chris.

    You're gonna go blind staring into that box all day.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: std::string as function parameter

    is std::string is a big structure needs using reference?
    Well...as dude_1967 already indicated...if a function does not change the contents of a variable you should basically pass them as a constant reference. However, this is only a matter while dealing with structures and/or classes. Simple data types can be still passed by value since passing by reference would only add unnecessary overhead.

    In addition, you might want to take a look at this article....

  4. #4
    Join Date
    Sep 2003
    Posts
    815
    and stl string is for that matter like basic type as int or double
    or like a class/struct??

    thanks

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    std::string is a class.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  6. #6
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    A reason to use const references instead of const pointers is that strings rely on overloaded operators to behave naturally. "str[5]" is certainly much more readable that "(*str)[5]".

    Jeff

  7. #7
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    Well, basically if you consider what has to be done then the answer will be obvious. If you pass by reference, the computer will have to copy the address of the paramter; 4 bytes on the 32bit system. Now if you pass by value, the whole string will have to be copied. So if the string is longer than 4 bytes it will be slower. In fact, since new memory has to be allocated, it will almost always be slower to copy and reference it. This goes for any other type as well, including a structure. If you structure is longer than 4 bytes it will be more effient to pass it by reference.

    Additional, const std::string means next to nothing. It creates a copy and doesn't allow you to modify it. That does make any sense, there is no reason you shouldn't be able to modify the copy. You should pass as const std::string&.

  8. #8
    Join Date
    Sep 2003
    Posts
    815
    if I can't use reference (that's one of the project rules)
    should I use a string or a pointer to a string?

    thanks

  9. #9
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    avi123,

    The pointer would still be preferable. Just use:

    Code:
    const std::string*
    and access the class methods with the -> operator.

    Sincerely, Chris.

    You're gonna go blind staring into that box all day.

  10. #10
    Join Date
    Sep 2003
    Posts
    815
    why does it have to do with the length of my string?
    no matter what the size of my string is sizeof(std::string) is 28
    which is 7 times 4, so I guess I should pass it with pointer...

  11. #11
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    avi123,

    The size of std::string is independent of the length of the string since the internal data are managed as a dynamically allocated pointer to character data. However, all copy operations operating on a given std::string will copy the internal data as well. Thus the run-time of the copy operations depends on the length of the std::string data.

    Sincerely, Chris.



    P.S. Like Dr. Ruth said, it's not the length of the std::string that matters, but rather how you use it...
    You're gonna go blind staring into that box all day.

  12. #12
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Well I just gained new respect for Dr. Ruth. I had no idea she was
    C++ programmer
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  13. #13
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    Originally posted by avi123
    if I can't use reference (that's one of the project rules)
    should I use a string or a pointer to a string?

    thanks
    One of these stupid assignments - why impose a rule that you can't pass a reference when that is a basic part of C++?

    Much of the time if you copy a string it will optimise such that the contents are copied only when one of them are modified.

    The purpose of this optimisation so that you can return a string as a return value from a function.

    There are still the overheads of creating a new class though - it is a different object, just the strings they point to are in the same memory space.

  14. #14
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Originally posted by avi123
    if I can't use reference (that's one of the project rules)
    should I use a string or a pointer to a string?
    If you can't use a reference, this must be an assignment, in which case the small optimization of passing by reference is unimportant.

    Pass by value. It will look cleaner.

    Jeff

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by avi123
    why does it have to do with the length of my string?
    no matter what the size of my string is sizeof(std::string) is 28
    which is 7 times 4, so I guess I should pass it with pointer...
    When you pass an object by value, the objects copy constructor is called. This makes perfect sense, since if the compiler is going to create a temporary object, it had better construct the object correctly. The only way to do this is to use the copy constructor.

    Since std::string's copy constructor copies the string contents from the current string to the newly constructed string, then you may see a performance hit if you pass a std::string that has 1,000,000 bytes of data by value.

    I say may see a performance hit, since some std::string implementations use reference-counting, so that copying from one string to another incurs little overhead. However, it doesn't take away from the fact that passing objects by value will incur a copy constructor call by the compiler.

    That is why it seems weird to me why it is "project rules" to pass objects by value, and not reference. Are the person(s) there you work with aware that passing everything by value may slow down your app (maybe to a crawl) if what they are passing has expensive (in terms of time) copy constructors? The role of a good C++ programmer is to know what can be passed by reference and by value, not just in terms of "what compiles", but also in terms of efficiency.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured