开发者

String assignment difference

What is the difference between METHOD 1 and METHOD 2?

#include <iostream>
#include &开发者_运维知识库lt;string>

using namespace std;
int main()
{
    // METHOD 1
    std::string name = "testing";
    std::string nameC = name;
    cout << "nameC is : " << nameC << endl;

    // METHOD 2
    char nameC1[8];
    const char* name1 = "testing";
    strlcpy(nameC1,name1,sizeof(nameC1));
    cout << "nameC1 is  : " << nameC1 << endl;
 }

Which is more efficient? Faster? When should one follow one over the other?


C++ way

std::string name = "testing";
std::string nameC = name;

In C++, resources are automatically handled. This is called RAII.

Here, the string can be considered as a "value type", that is, something that, like an int or a double, can be safely copied, modified, returned, passed as a parameter, etc., without risk. It just works.

The RAII part means the string has an internal pointer to its content (i.e. "testing"). When you copy the string, the content is copied. When you exit the scope, the string is destroyed, automatically cleaning/deallocating the content. No memory leaks, here.

Optimizations exist, depending on implementations. For example, you could have COW, meaning the copy above would be similar to a pointer copy. In other cases, the string contains a small buffer, meaning small strings don't need heap allocation/deallocation.

Your mileage can vary, but the important points here are:

  • RAII
  • it just works

C way

//METHOD 2**
char nameC1[8];
const char* name1 = "testing";
strlcpy(nameC1,name1,sizeof(nameC1));

In C, there are no such automated behaviors, so everything must be done by hand, and one modified line of code can break another without the compiler telling you.

In the current case above, as nameC1 is an array on stack, you can't return it. This is quite a drawback, but in the other hand, it means no allocation on heap.

Another drawback of the C way is that you must use strlcpy because you don't want to have a buffer overrun (for example, if name1 was "testingtestingtestingtesting" instead of "testing"), so you must supply the size of the receiving string. Here, with the array, this is almost easy, as a sizeof will do the trick (but change the type from char to wchar_t, and the sizeof part will need to modified...)

But if the string type nameC1 was changed to char * (because, for example, you want to return it from a function), then you would have to potentially (re)allocate its memory to have "testing" fit inside, meaning that you need to know the length of name1 (which can be costly, and/or cumbersome), and then, not forget to deallocate it when not needing it anymore.

So:

  • the code is brittle, as it depends on the types used. Change one thing, and the code is broken
  • the code is limited, as you can't return an array declared on the stack
  • the code is possibly broken, as your receiving buffer can't hold more than 7+1 characters

Conclusion

The C code above is quite brittle.

Change one thing, and the others stop to work properly. For example, changing the array into a pointer will break the strlcpy because it relies on a sizeof (which, in case of a pointer, will return the wrong value).

And even as such, the code could be considered as broken by the end-user because you won't put more in the C array than 7 characters plus the \0, meaning the string will possibly be truncated.

Those problems don't exist in the C++ version of the code: The lines of C++ code will handle all and every cases, naturally.

So, the conclusion is that you should std::string as much as possible in your code, because it will automate things at zero or marginal cost.

And in the rare cases when you need faster processing (usually, you want to remove the heap allocation/deallocation parts), then you modify the code as needed (usually by allocating a fixed-size buffer, accepting its limitations in exchange of its strengths).


Use Method 1.


In the long run, Method 1 is:

  • Easier to use - you don't have to manually create clunky C-string implementations.
  • More readable.
  • C++.

Method 2 is:

  • Harder to use.
  • Less readable.
  • C.

As for the 'faster', either Method could be, depending on your implementation/etc. The minute potential difference doesn't really matter anyways, in the face of the other points listed above.


Method1 is C++ Metthod2 is C

If you program is C++ (as indicated by the C++ tag of your question), use method 1: It helps code more easily string manipulations and allows for better flexibility of objet lifetime. It will also allow for better interoperability with other C++ objects such as collections.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜