From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,243dc2fb696a49cd X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!nntp.idg.pl!newsfeed.atman.pl!newsfeed.tpinternet.pl!atlantis.news.tpi.pl!news.tpi.pl!not-for-mail From: "Robert Kawulak" Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) Date: Tue, 28 Sep 2004 19:46:56 +0200 Organization: tp.internet - http://www.tpi.pl/ Message-ID: References: <1777528.JKnUEYTOM6@linux1.krischik.com> <1ec946d1.0409230820.455ad242@posting.google.com> <3673998.bj16mkkOu2@linux1.krischik.com> <1700922.2nPlMsa4Ny@linux1.krischik.com> <3750337.aJMmIeXcav@linux1.krischik.com> NNTP-Posting-Host: pk133.krakow.cvx.ppp.tpnet.pl X-Trace: atlantis.news.tpi.pl 1096409087 28775 217.99.211.133 (28 Sep 2004 22:04:47 GMT) X-Complaints-To: usenet@tpi.pl NNTP-Posting-Date: Tue, 28 Sep 2004 22:04:47 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original Xref: g2news1.google.com comp.lang.ada:4343 Date: 2004-09-28T19:46:56+02:00 List-Id: Hi, >>> And just for the record: atio is a C function - you need the overhead of >>> convertion the std::string into a char* and std::strstream seem quite >>> heavy >>> weight for just a simple task of convertion a string from and to an int. >> >> Actually, the overhead of conversion is virtually none (at least in >> Stlport implementation). You just call std::string::c_str() which just >> returns a pointer to the data, and which is also inlined. > > From what I read about std::string strings do not need to be '\0' > terminated > and std::string::c_str() will add a terminator if needed. Thats unlike > std::string::data() which does not add a '\0'. Yup, you're absolutely right. But Stlport implementation always stores '\0' at the end, so std::string::c_str() boils down to returning a pointer. On the other hand, the GCC implementation writes terminating '\0' every time this function is called. However, this is still constant complexity and a little overhead contrary to what many people suspect - that converting std::string to char * requires copying whole the contents of the string to some temporary buffer. > Well on the other side > std::string::c_str() is const. c_str() may modify the data even despite being const if the data is declared with mutable specifier. And that's how it's done in GCC. > This all leaves me wonder how actually works. And if I ever need to work > again if I had a Euro for every call where std::string::data() and > std::string::c_str() have been mixed up. At the beginning I was also confusing the two. But since I've read the documentation carefully, I always remember the difference... ;-) >> void f() { >> >> //instead of std::string::fromInt(int) >> oss MyOutputStream; >> MyOutputStream << i; >> MyString = MyOutputStream.str(); >> >> //instead of std::string::toInt() >> iss MyInputStream; >> MyInputStream.str(MyString); >> MyInputStream >> i; >> >> } //f() > > Well, nice. But are you shure you don't leak memory here. I remember there > was something about ownership and the str() function. I don't know which place exactly you mean, but I'm pretty sure it all works as supossed. Strings, streams and all other objects of std library classes take care for their storage themselves - a user doesn't have to worry about this. Coud you be more specific? >> //same things as one-liners: > > Please DON'T. This is comp.lang.ada after all. Three liners are quite OK. OK, I promise this won't happen anymore! ;-) > Not only that you can shoot yourself in the foot with C++ (You can do that > with Ada as well) but the weapon is allways loaded (that is, cannot be > unloaded), has no safety lever, and the trigger is allwas taut. And of > corse it is a snipers hair trigger going off at the slightest touch. > > So no matter how experienced you are, you are allways in danger of > shooting > your food of. I can't agree with that at all. In C++ there is almost always an alternative. You may use std::vector<> instead of arrays, std::auto_ptr<> instead of naked pointers etc. But still you have a choice - if you really need to have a total low-level control on your program, you may have it. Otherwise use higher abstraction-level utilities. What you say might be eventually true for C, but not for C++. And the difference between the two is enormous, unfortunately many people think C and C++ is the same... Best regards, Robert