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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news2.google.com!proxad.net!newsfeed.stueberl.de!newsfeed01.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) Date: Wed, 29 Sep 2004 09:49:08 +0200 Organization: AdaCL Message-ID: <3060085.UHp9MgIIe7@linux1.krischik.com> 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> Reply-To: krischik@users.sourceforge.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.t-online.com 1096445310 04 6817 trBkXIHl-c-jth 040929 08:08:30 X-Complaints-To: usenet-abuse@t-online.de X-ID: rkqIG-ZAQeA+6QhFoZmF3U5-GsoXdH5PjBkwgP0GlFIuWaPcK6B44x User-Agent: KNode/0.8.0 Xref: g2news1.google.com comp.lang.ada:4364 Date: 2004-09-29T09:49:08+02:00 List-Id: Robert Kawulak wrote: > Hi, > >>>> And just for the record: atio is a C function - you need the overhead >>>> of convertion the std::strivalidto 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. But that means that the strings need "capacity () - size () >= 1" so that there is allways one spare byte for the '\0'. >> 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. I was thinking about "capacity () - size () == 0" - then c_str() would need to resize. Doesn't happen in your implementation - but is this valid for all. >> 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? I might me mistaken with the old strstream. >>> //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... Not quite true. I did have to make Ada::Range myself. Actually, I still have to make it. My Current implementation is for the old standart (as in Ada::IntegerRange). C++ has protection against buffer overruns - but the new JPEC Viruses work integer overurns. And there is no: typedef explicit int My_Int. And that means that typedef Ada :: Range US_Hours; typedef Ada :: Range Month; are the same for C++. But there are not in real live. And if I use: class US_Hours Ada :: Range {}; class Month Ada :: Range {}; I loose the constructors. So there is still something to be improved. With Regards Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com