On Wed, 16 Oct 2013 02:34:17 +0200, "Alf P. Steinbach"
<
alf.p.stein...@gmail.com> wrote:
>On 16.10.2013 00:32, DSF wrote:
>> On Mon, 14 Oct 2013 21:53:25 +0200, "Alf P. Steinbach"
>> <
alf.p.stein...@gmail.com> wrote:
>>
>>> On 14.10.2013 21:07, DSF wrote:
>
>Yep. The standard library's "char" based string type is "std::string".
>There's also a "wchar_t" based string type called "std::wstring".
>
>Both these types are just instantiations of the "std::basic_string"
>class template, which primarily means that in some help files and
>documentation sets you have to look up "std::basic_string".
It wouldn't have helped. As I said below all the STL information
was in a separate help file. (Perhaps I should have said "found one
for STL that is totally separate and not linked to by the main help
file.")
>
>> Yet I know
>> my compiler can handle STL code. I browsed through the numerous help
>> files and found one for STL. So I shall now try to interpret the
>> above code.
>>
>> assert( buf_size > 0 );
>> std::string s( buf_size, '#' );
>>
>> These lines assume (and test for) the existence of a buffer of
>> undetermined type (I assume char) and of a known length (buf_size).
>
>Well I meant "buf_size" as just a required buffer size, like
>
> int buf_size = 42;
I understand.
>
>> string s is created and filled buf_size number of the character #.
>
>Yes.
>
>This includes that it allocates a buffer of at least the specified size.
>
>The buffer is automatically deallocated when the string ceases to exist.
Yup. And that's one thing my string class doesn't have (yet), the
ability to pre-allocate string space. It will increase space as
needed, and even de allocate space if allocated_space - string_size
reaches a predetermined difference.
>
>> The next line made no sense, then made a little more sense, but
>> still makes no sense.
>>
>> auto const new_length = some_c_function( &s[0], s.size() );
>>
>> The first point of confusion was the use of "auto," but research led
>> to the new C++11 definition of the keyword. So we are creating a
>> constant variable of type to be determined by whatever some_c_function
>> returns.
>
>Yes.
I can see where this can make the code less clear. At the least,
one will have to get used to tracking down the function declaration
for its return type instead of the variable's declaration. In my
crystal ball I see "auto" variables having names that accurately
describe their purpose.
That's one thing I've noted with C++, you have to pay more attention
to the "big picture." In c, a = b + c; is fairly straightforward, but
in C++, it could mean, or do, anything!
(I've often wondered if spy organizations write in C++. In the
above statement "+" could be overloaded to launch missiles, erase
data, anything.)
Many Windows functions provide a mechanism by which you call the
function with the output string pointer set to NULL, causing it to
return the length of the buffer needed. Others use the "loop until
error is not 'buffer too small'" approach. Some provide neither. They
are the pain in the a$$ ones.
(I had to rewrite printf for my compiler (no wchar_t support) so I
added a feature to SPrintf(char *buf, const char*format, ...) so that
if buff is NULL, it returns the length needed. Yes, I still use
printf, but that's a whole different topic.)
I have to deal with calls like:
int SplitPathComponents(const char *path, char *drive, char
*directory, char *filename, char *extension);
That's four non-const outputs to deal with. Fortunately three of
them have OS set limits, but you get the idea.
>The buffer size is first obtained, and then used to create a buffer of
>the requisite size for the second call.
>
>In general one will just have to do whatever the C function at hand
>permits and suggests.
As I agreed above.
>
>> It also requires an existing string of buf_size length, which is the
>> same problem with using a temporary c-string; the need to know or
>> guess how long of a string some_c_function will return.
>
>Yes.
>
>But by using std::string you avoid having to deal with copying and
>deallocation.
The same with my string class. One thing I can't do with it is in
the statement:
some_c_function( &s[0], s.size() );
&s[0] works with STL string, but produces the error "Must take address
of a memory location." I expect to learn some interesting things
while exploring the difference.
{bool tongue_in_cheek = true;}
This may get me flamed out of the group, but I actually like working
with c-strings. Need the string shorter? Just pop in a zero at the
right spot and you're done!
{tongue_in_cheek = false;}
Seriously, I do enjoy c-string manipulation on a lower level and
have accumulated a large library of C/ASM string routines, creating
new tools as I've needed them.
I've been integrating them into my string class, once again as
needed.
>
>Cheers & hth.,
>
>- Alf
DSF