D
Divick
Hi all,
I have a problem related to std::string class. Is it ok to
assign a global string variable to a local string object as shown
below?
I am trying to print the address of local string buffer and then I
print the address of global string buffer and come out to be same, but
I assume that as soon as func method has returned that temporaryString
string must have been deleted and thus the buffer that it might be
holding might get freed unless that buffer is being reused by the
string class.
So doing something like below might depend on the implementation of
std::string class. Can I assume that it would do the right thing?
string globalString;
void func()
{
string temporaryString = "some value";
printf("Address of local strings buffer %x\n",
temporaryString.c_str());
globalString = temporaryString;
return;
}
void someOtherFunc()
{
func();
//Now use the globalString but the address printed below is
same as the one
//printed by the function func which might get deleted /freed
by string class
printf("Address of global string buffer
%x\n",globalString.c_str());
}
Another question I have is related to STL. I was storing char * 's in
STL containers previously, since now I am converting all my char *'s to
string to avoid memory leaks, I wanted to know that shall I make
container of string or container of string references.
For example:
map<const string&, const string &, myComparator>
or
map<const string, const string , myComparator>
Is there some guidelines for that?
Thanks,
Divick
I have a problem related to std::string class. Is it ok to
assign a global string variable to a local string object as shown
below?
I am trying to print the address of local string buffer and then I
print the address of global string buffer and come out to be same, but
I assume that as soon as func method has returned that temporaryString
string must have been deleted and thus the buffer that it might be
holding might get freed unless that buffer is being reused by the
string class.
So doing something like below might depend on the implementation of
std::string class. Can I assume that it would do the right thing?
string globalString;
void func()
{
string temporaryString = "some value";
printf("Address of local strings buffer %x\n",
temporaryString.c_str());
globalString = temporaryString;
return;
}
void someOtherFunc()
{
func();
//Now use the globalString but the address printed below is
same as the one
//printed by the function func which might get deleted /freed
by string class
printf("Address of global string buffer
%x\n",globalString.c_str());
}
Another question I have is related to STL. I was storing char * 's in
STL containers previously, since now I am converting all my char *'s to
string to avoid memory leaks, I wanted to know that shall I make
container of string or container of string references.
For example:
map<const string&, const string &, myComparator>
or
map<const string, const string , myComparator>
Is there some guidelines for that?
Thanks,
Divick