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.
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....
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
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.
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]".
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&.
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...
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.
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.
Bookmarks