STL string and assignment operator. Plz Help.

D

developer28

Hi,

I would like to know the following.

suppose i write.

string str1 = "TestString";

string str2;

str2 = str1

Now can someone tell me how this assignment operator works internally.

actually i do something like

const char * temp = str1.c_str();

that converts it to a c type string.

and then when i modify the contents of temp, it also affects str2.

Now this makes me doubtful about the fact that internally both the
objects are using the same pointer.

Please forgive me if my query is too trivial.

Thanks for any help.

Regards,
Aman.
 
M

Markus Moll

Hi
actually i do something like

const char * temp = str1.c_str();

that converts it to a c type string.

and then when i modify the contents of temp, it also affects str2.

You are not allowed to change the contents through a pointer obtained from
c_str(): "const charT* c_str() const; [...] Requires: The program shall not
alter any of the values stored in the array. [...]" (There is a reason why
c_str() returns a pointer to _const_ char)

Probably your implementation uses copy-on-write and you screw things up;
technically you have undefined behavior anyway.

Markus
 
G

Guest

Hi,

I would like to know the following.

suppose i write.

string str1 = "TestString";

string str2;

str2 = str1

Now can someone tell me how this assignment operator works internally.

No, since I do not know how std::string (on your implementation) works.
But I can make an educated guess.
actually i do something like

const char * temp = str1.c_str();

that converts it to a c type string.

and then when i modify the contents of temp, it also affects str2.

Now this makes me doubtful about the fact that internally both the
objects are using the same pointer.

They are not using the same pointer, each string uses separate data. A
simplified string class might look something like this:

class String
{
int size;
char* data;
public:
String& operator=(const String& s)
{
delete data;
data = new char[s.size];
strcpy(data, s.data);
size = s.size;
}
};

In other words the assignment operator creates a copy of the string.
 
B

Barry

developer28 said:
Hi,

I would like to know the following.

suppose i write.

string str1 = "TestString";

string str2;

str2 = str1

Now can someone tell me how this assignment operator works internally.

actually i do something like

const char * temp = str1.c_str();

that converts it to a c type string.

and then when i modify the contents of temp, it also affects str2.

Now this makes me doubtful about the fact that internally both the
objects are using the same pointer.


[STD --
21.3.1 basic_stringconstructors

basic_string<charT,traits,Allocator>&
operator=(const basic_string<charT,traits,Allocator>& str);

16 Effects: If *this and str are not the same object, modifies *this as
effects:

data() points at the first element of an
allocated copy of the array whose
first element is pointed at by
str.data()

size() str.size()

capacity() a value at least as large as size()

If *this and str are the same object, the member has no effect.
--End-STD]


So std::string (basic_string<char>) does not share data with different
instances.

So the case you described here is not likely to happen if the
implementation is standard-conformed.
 
C

Chris ( Val )

Hi,

I would like to know the following.

suppose i write.

string str1 = "TestString";

string str2;

str2 = str1

Now can someone tell me how this assignment operator works internally.

Sorry, I can't help you with that.
actually i do something like

const char * temp = str1.c_str();

that converts it to a c type string.

Modifying a const char* is undefined behaviour, and that
means that any results you see are possible, albeit right
or wrong.
 
A

Andre Kostur

internally.

No, since I do not know how std::string (on your implementation) works.
But I can make an educated guess.

How? It's a pointer to const char. You can't modify it without casting
away the constness, and at that point you've entered the realm of
Undefined Behaviour.
 
J

James Kanze

I would like to know the following.
suppose i write.
string str1 = "TestString";
string str2;
str2 = str1
Now can someone tell me how this assignment operator works internally.

No. It varies from one implementation to the next.
actually i do something like
const char * temp = str1.c_str();

Which is something completely different.
that converts it to a c type string.
and then when i modify the contents of temp, it also affects str2.

I doubt it. You mean that if later, you do something like:

temp = NULL ;

, the contents of str2 (or str1) change? I've never seen
anything like that, and I don't see how it could be.

Technically, you cannot modify anything through temp, since it
points to const. The next version of the standard will have a
non-const data() function, however, which is expressedly
designed to allow modification of the string, e.g. by legacy C
functions which take char* as pointers to a buffer. In
practice, you can reliably use this today as well, by casting
away the const on the pointer returned by data(), or by using
&str[0]. It is, however, very dangerous; any modifications
beyond str.size() will result in immediate undefined behavior.
It should only be used when interfacing with legacy code.

And of course, what happens in str.data() has nothing to do with
assignment.
Now this makes me doubtful about the fact that internally both
the objects are using the same pointer.

The specifications of std::basic_string are fairly complicated,
intentionally, in order to allow different implementations. The
user visible semantics, however, are fairly clear, and that's
all you need to worry about.
 
J

Jim Langston

developer28 said:
Hi,

I would like to know the following.

suppose i write.

string str1 = "TestString";

string str2;

str2 = str1

Now can someone tell me how this assignment operator works internally.

Internally most likely the bytes that represent the string in str1 are
copied into str2 byte for byte.
actually i do something like

const char * temp = str1.c_str();

Here you are not using std::strings copy constructor, you are using a const
char* copy constructor. The only thing that is copied is the value of the
pointer that c_str() returns.
that converts it to a c type string.

No, that returns a pointer to a c-style string which is still internal to
str1
and then when i modify the contents of temp, it also affects str2.

And this is not allowed, This will provoke undefined behavior. First off,
it's a const char*, constant, you are not allowed to change the contents.
Second, it is a temporary pointer, as soon as str1 changes for whatever
reason that pointer can become invalidated. Now, being undefined behavior
it can pretty much do anything, including change the original string. Or
change str2. Or str99283. It is undefined behavior. Don't do it.
Now this makes me doubtful about the fact that internally both the
objects are using the same pointer.

Both of which objects? str1 and str2? They should be using their own
pointers to their own sets of data.
 
J

Juha Nieminen

developer28 said:
string str1 = "TestString";

string str2;

str2 = str1

Now can someone tell me how this assignment operator works internally.

It depends on the compiler.

Some compilers implement std::string copying by using the
copy-on-write technique. This means that only the pointer is copied and
a reference count incremented. (The deep copy is made only if you try to
modify one of the strings.)

Other compilers do a deep copy.

The standard doesn't enforce any specific method to be used.
actually i do something like

const char * temp = str1.c_str();

that converts it to a c type string.

and then when i modify the contents of temp, it also affects str2.

As people have told you, you shouldn't do that. You are modifying
the data behind a const pointer, which is asking for trouble.

Anyways, this would indicate that the compiler you are using is using
copy-on-write for std::string (and you are circumventing it with your
modifying-const-data hack).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top