-
December 7th, 2003, 03:36 AM
#1
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
-
December 7th, 2003, 06:45 AM
#2
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.
-
December 7th, 2003, 08:12 AM
#3
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....
-
December 7th, 2003, 08:23 AM
#4
and stl string is for that matter like basic type as int or double
or like a class/struct??
thanks
-
December 7th, 2003, 09:57 AM
#5
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.
-
December 7th, 2003, 11:10 AM
#6
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
-
December 8th, 2003, 12:38 AM
#7
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&.
-
December 8th, 2003, 02:12 AM
#8
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
-
December 8th, 2003, 02:16 AM
#9
avi123,
The pointer would still be preferable. Just use:
and access the class methods with the -> operator.
Sincerely, Chris.
You're gonna go blind staring into that box all day.
-
December 8th, 2003, 02:49 AM
#10
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...
-
December 8th, 2003, 03:06 AM
#11
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.
-
December 8th, 2003, 03:39 AM
#12
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."
-
December 8th, 2003, 06:55 AM
#13
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.
-
December 8th, 2003, 10:24 AM
#14
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
-
December 8th, 2003, 12:06 PM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|