Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::string::assign takes "ownership" of the string?

Tags:

c++

string

I have some gaps in the understanding of string::assign method. Consider the following code:

char* c = new char[38]; strcpy(c, "All your base are belong to us!"); std::string s; s.assign(c, 38); 

Does s.assign allocate a new buffer and copy the string into it or it assumes ownership of the pointer; i.e. doesn't allocate new memory and uses directly my address. If it copies, then what is the difference between assign and operator=? If it doesn't copy, then does it free the memory or it is my responsibility?

Thank you.

like image 629
FireAphis Avatar asked Jul 12 '10 13:07

FireAphis


People also ask

What does std::string () do?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

Is std::string the same as string?

There is no functionality difference between string and std::string because they're the same type.

How is a string allocated in C++?

The code calls operator new[] to allocate memory for 10 string object, then call the default string constructor for each array element. In the way, when the delete operator is used on an array, it calls a destructor for each array element and then calls operator delete[] to deallocate the memory.

What type is std::string?

The std::string type is the main string datatype in standard C++ since 1998, but it was not always part of C++. From C, C++ inherited the convention of using null-terminated strings that are handled by a pointer to their first element, and a library of functions that manipulate such strings.


2 Answers

Does s.assign allocate a new buffer and copy the string into it or it assumes ownership of the pointer;

The STL string method assign will copy the character array into the string. If the already allocated buffer inside the string is insufficient it will reallocate the memory internally. The STL string will not take ownership of the original array.

If it copies, then what is the difference between assign and operator=?

Both ought to act in the same way, but there are a number of overloads to the STL assign method which give you more control over what happens. Take a look at this page for more information.

UPDATE: The MSDN has a number of examples of the various assign overloads.

If it doesn't copy, then does it free the memory or it is my responsibility?

No, the original pointer to the character array is still your responsibility.

like image 51
Konrad Avatar answered Sep 30 '22 19:09

Konrad


It copies. The difference between assign and operator= is that you can specify the number of characters to be copied including null characters. The operator= just copies the c-string up to the first null byte.

like image 37
A. Levy Avatar answered Sep 30 '22 20:09

A. Levy