Re: CString help



On Thu, 21 Jun 2007 22:25:43 -0700, "Mihai N." <nmihai_year_2000@xxxxxxxxx>
wrote:

CString::GetBuffer() can increase efficiency in some situations, but is
best avoided, IMHO. You will notice that std::string has no such mechanism.

To be avoided, but still best to have it.
I think it is bad that std::string does not have such a thing.
See my "std::string internal buffer access" note at
http://www.mihai-nita.net/article.php?artID=20060430a

Practically speaking, you can do this:

const int N = 100;
std::string s;
s.resize(N);
int len = WindowsAPI(&s[0], N);
s.resize(len);

Though not guaranteed, I don't know any implementation where this won't
work. (I'd really like the non-contiguity, reference-counted, etc options
to just go away. People who need those things can't do without them, and
the possibility they exist complicates things for everyone else.)

Aside: MSDN has gotten a lot better about defining the meaning of N in the
code above. As a function parameter, it usually indicates the maximum
number of characters that may be written to the buffer, including the
terminating nul. As a return value, it usually indicates the number of
characters actually written, excluding the nul. This is true for functions
like GetWindowText, which truncate the returned text as necessary. What
does this imply for the return value?

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: CString help
    ... string values) the size of the buffer, you need to double the buffer and read again, ... As a function parameter, ... terminating nul. ... characters actually written, excluding the nul. ...
    (microsoft.public.vc.mfc)
  • Re: Acquiring UTF-8 string length
    ... http://en.wikipedia.org/wiki/Multi-byte_character_set, this UTF-8 string ... The number of codepoints is four, plus the terminating NUL. ... Four characters plus a terminating NUL. ...
    (microsoft.public.vc.language)
  • Re: FreeBSDs malloc problem ?
    ... charallocates exactly 16 characters. ... the terminating nul will slop over ... round up the number of bytes to something convenient. ... 16 is pretty small -- the linux malloc might round ...
    (freebsd-hackers)
  • Re: about string and character
    ... the number of characters before the terminating nul. ... How does that result differ from the index of the first '\0' in a string? ...
    (comp.lang.c)