question about std::string/map

L

lallous

Hello,

I have:

std::map<std::string, std::string> m;
std::string s1;
char *p = "hello world", *p1 = "value";
(a) s1 = p;

is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
string now?

(b) m[p] = p1;

Is that okay? will that create a valid entry into the map?
Or should I use: map[s1] = "value"; ?

Btw, can someone tell me about std::string and its reference count issues?
 
K

Kamil Burzynski

std::map<std::string, std::string> m;
std::string s1;
char *p = "hello world", *p1 = "value";
(a) s1 = p;

is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
string now?

s1 will do its own copy
(b) m[p] = p1;

Is that okay? will that create a valid entry into the map?
Or should I use: map[s1] = "value"; ?

In m[p]=p1, at first string will be created with copy of p, then m[] will
get is as argument, second string will be created with copy of p1 and
then assigned to m[p]

This is equivalent:
string sp( p );
string sp1( p1 );
m[ sp ] = sp1;
Btw, can someone tell me about std::string and its reference count issues?

AFAIK std::string does not have to do reference counting (but it is
allowed to). If it does share string between its instances, it is
copy-on-write algorithm.
 
S

Sharad Kala

lallous said:
Hello,

I have:

std::map<std::string, std::string> m;
std::string s1;
char *p = "hello world", *p1 = "value";
(a) s1 = p;

is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
string now? Yes


(b) m[p] = p1;

Is that okay? will that create a valid entry into the map? Yes
Or should I use: map[s1] = "value"; ? No
Btw, can someone tell me about std::string and its reference count issues?

That's implementation dependent.
Say p1 and p2 are string objects and both contain the string "Sharad".
Then an implementation could use reference counting till no modifying operation
occurs on either of the strings.
Read Scott Meyers MEC++ which talks about such cases of reference counting.

Best wishes,
Sharad
 
K

Kevin Goodsell

lallous said:
Hello,

I have:

std::map<std::string, std::string> m;
std::string s1;
char *p = "hello world", *p1 = "value";

Don't do this. It makes use of a dangerous and deprecated language
feature - the implicit conversion of a string literal to a (non-const)
char pointer. Always use 'const char *' or 'const char *const' for
pointing to a string literal. This way the compiler will warn you if you
attempt to modify the string literal. Without it, your code will
silently invoke undefined behavior if you attempt to modify a string
literal.
(a) s1 = p;

is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
string now?

(b) m[p] = p1;

Is that okay? will that create a valid entry into the map?
Or should I use: map[s1] = "value"; ?

Btw, can someone tell me about std::string and its reference count issues?

I don't believe there are any such issues. Whether or not std::string
uses reference counting is an implementation issue, and transparent to
the programmer.

-Kevin
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top