[Xapian-devel] sizeof(std::string)
Olly Betts
olly at survex.com
Thu Apr 5 15:39:57 BST 2007
Currently we carefully try to pass std::string by const reference
everywhere, which is a good idea if one assumes it's an object of
non-trivial size. Bjarne Stroustrup's guideline is "more than a
couple of words):
http://www.research.att.com/~bs/bs_faq2.html#call-by-reference
I've noticed that under GCC at least (I tried 2.95, 3.3, and 4.1)
std::string just holds a pointer to the actual implementation so
sizeof(std::string) == sizeof(void*).
How big is it elsewhere? It would be interesting to see what this
program outputs under other compilers:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cout << sizeof(s) << endl;
}
If std::string is typically implemented this way, there's much less
point in passing it by const reference as that is probably slightly
slower!
Even if the implementation is stored in the string object directly,
I suspect it's likely to consist of a pointer and two size_t members
(length and allocated length) which is only 3 words...
Not suggesting we change this for 1.0 though regardless!
Cheers,
Olly
More information about the Xapian-devel
mailing list