Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Differences between C and C++ Date: Sun, 31 Jul 2022 17:03:52 -0700 Organization: None to speak of Lines: 76 Message-ID: <871qu0x21j.fsf@nosuchdomain.example.com> References: <8699b402-e8e2-4e85-8de6-d67249cbeca9n@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="9f4a8c8a3c572d647fa545652ca10113"; logging-data="556309"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WT21GwQpQhTsYBage7r3X" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:6lWQiBW5B08x6hYUAzAxC1fM1DI= sha1:WkmJIjI+bstNSWTwyzdNX2MIJEM= Xref: csiph.com comp.lang.c++:85768 Malcolm McLean writes: > On Sunday, 31 July 2022 at 17:49:22 UTC+1, F.Zwarts wrote: >> Op 31.jul..2022 om 16:39 schreef Juha Nieminen: >> > Manfred wrote: >> >> That, and below, is generally true for a generic dynamic container. >> >> However, std::string is not a generic container. It is a very specific >> >> and very optimized container of chars. >> >> So, if you use a decent implementation of std the performance of >> >> std::string and char* is usually pretty close. >> > >> > I don't know if you are referring to it, but many people seem to think that >> > "short string optimization" makes std::string pretty much as efficient >> > as an array of char (for strings that are short enough). >> > >> > They fail to take into consideration that short string optimization >> > requires conditionals in almost all member functons that access the >> > string data. Conditionals are not free. >> I wonder if that is true. The string object has a pointer to its buffer >> and a length. I assume that if the short string optimization is used, >> this pointer points to the internal buffer. Only member functions that >> want to increase the size of the buffer need those conditionals. I >> wonder whether "almost all member functions" modify the buffer size. >> > The std::string is (in C) > struct string > { > char *buff; > size_t len; > } > so 16 bytes on a 64-bit machine. That's one possible implementation. > However in reality not all 64 bits of a pointer are wired to memory addresses. > It's likely that the most significant bit has to be clear in a valid address. > So we can exploit this by (in C) > struct shortstring > { > bool flag; // set when the shortstring is valid > int pad:7; // Maybe use for length or encoding or other stuff > char data[15]; // UP to 14 characters of ASCII string data. > }; > > union > { > struct string s; > struct shortstring ss; > } std_string; > > Now string::size is implemented as > if(s->ss.flag) > return strlen(s->ss.data); > else > return s->s.len; > > > The other string member functions are implemented similarly. That won't work without extra code to ensure that the short string optimization isn't used in all cases. The length of a std::string is not determined by a null character. This program must print 3 : #include #include int main() { std::string s = "a"; s += '\0'; s += 'b'; std::cout << s.size() << '\n'; } -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */