Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use pointer to std::string

Tags:

c++

pointers

std

qt

In learning c++, I first use Qt library instead of the standard C++, STL and all that (Ok, so I'm new with c++ and spoiled by Qt). On Qt, QString used implicit sharing, thus enabling me to just copy assign it to another variable like:

QString var1=QString("Hi there!");
QString var2=var1

And that would do nicely without much overhead. But now, i'm trying std::string so, should I do

std::string var1=std::string()

or

std::string* var1=new std::string()

And also, how about QVector and std::vector. And If I do have to use the pointer... any tips?

like image 830
asdacap Avatar asked Oct 30 '11 15:10

asdacap


People also ask

Is std::string a pointer?

The c_str method of std::string returns a raw pointer to the memory buffer owned by the std::string . The pointer is only safe to use while the std::string is still in scope. When the std::string goes out of scope, its destructor is called and the memory is deallocated, so it is no longer safe to use the pointer.

Can we use pointer to string in C++?

Strings can also be declared using pointers. Let's see an example. In the above example, since p stores the address of name[0], therefore the value of *p equals the value of name[0] i.e., 'S'. So in while loop, the first character gets printed and p++ increases the value of p by 1 so that now p+1 points to name[1].

Is std::string good?

In general you should always use std::string, since it is less bug prone. Be aware, that memory overhead of std::string is significant. Recently I've performed some experiments about std::string overhead. In general it is about 48 bytes!

Why do we use pointers in strings?

A pointer to string in C can be used to point to the base address of the string array, and its value can be dereferenced to get the value of the string. To get the value of the string array is iterated using a while loop until a null character is encountered.


4 Answers

Whether std::string uses copy-on-write depends on the implementation (i.e. your standard library vendor decides that). However, most std::string implementations will not use COW, largely due to the fact that most if not all read operations force a copy -- operator[] returns a reference, c_str() and data() return a pointer. Compare this to QString::operator[], which returns a proxy object.

In spite of the above, don't use pointers to std::string, unless you determine (by measuring) that string copies are the bottleneck in your application.

Also, beware that QString stores UTF-16 strings, whereas std::string stores a sequence of chars -- QByteArray would be the Qt equivalent.

like image 117
avakar Avatar answered Oct 21 '22 13:10

avakar


std::string var1("Hi there!");
std::string var2=var1; 

std::string class has an = operator defined as:

string& operator= ( const string& str );
like image 23
Alok Save Avatar answered Oct 21 '22 14:10

Alok Save


std::string* var1=new std::string()

Don't do that. Just pass it by reference wherever possible:

void f(const std::string& s); // no copying

If you really need to share the string, use:

std::shared_ptr<std::string> var1 = std::make_shared<std::string>();
like image 25
Yakov Galka Avatar answered Oct 21 '22 12:10

Yakov Galka


If you are going to return std::string from function then don't use pointer - return by value. In this case most likely Return Value Optimization will be applied and string data will not be copied.

like image 38
ks1322 Avatar answered Oct 21 '22 12:10

ks1322