Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl)

From: Robert Kawulak (tigrisek_at_interia.pl)
Date: 09/28/04


Date: Tue, 28 Sep 2004 19:46:56 +0200

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



Relevant Pages

  • Re: ptr conversions and values
    ... >> each pointer pointed to a byte, ... Without that conversion, a pointer that points to an N-byte ... pointers to incomplete array types must indicate the start of the array ... >> It's all right, mostly equivalent with the previous one, representation ...
    (comp.std.c)
  • Re: typecast internals
    ... (snip of description of floating point formats) ... >>that the value is of the appropriate pointer type, ... > much of a conversion operation as arithmetic-type casts. ...
    (comp.lang.c)
  • Re: C variable retyping
    ... whose type is given by the cast. ... >an integer, no conversion is done, and c is used, within the ... In other words, you may take a pointer value, then use a cast to ... which is probably why the C99 folks decided to allow it ...
    (comp.lang.c)
  • Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl)
    ... > pointer. ... the GCC implementation writes terminating '\0' ... >> that with Ada as well) but the weapon is allways loaded (that is, ...
    (comp.lang.ada)
  • Re: ptr conversions and values
    ... >>> pointer points to an object consisting of a number of bytes. ... Note that I'm not relying on any conversion here, ... It's all right, mostly equivalent with the previous one, representation ... There are no values of an array type. ...
    (comp.std.c)