G
Guest
If a size_t is cast to a long, and size_t is the length of a unicode string, does the resulting long need to be divided by sizeof(_TCHAR) in order to get the actual length in _TCHARs?
songie said:If a size_t is cast to a long, and size_t is the length of a unicode
string, does the resulting long need to be divided by sizeof(_TCHAR)
in order to get the actual length in _TCHARs?
songie said:ok Carl.
maybe you can help me:
I've got a string (of _TCHARs), for instance
_TCHAR* mystring = "quick brown fox jumps over lazy dog";
I then use this:
wordlen = (long)_tcsspn(mystring, "abcdefgh...wxyz");
(where the second argument is the whole alphabet but without space)
which of course returns 6. This is what I would expect, as 6 is the
position of the first character that ISN'T an alphabetic character,
i.e.
a space.
I now want to extract the word "quick" and copy it into a string of
its own.
For this I'm allocating a dynamic array of _TCHARs on the heap (I
don't
know how long the word might be).
using:
_TCHAR* word = new _TCHAR[wordlen];
_tcsncpy(word, mystring, wordlen);
...<do some operations on the 'word' variable>
delete[] word;
It returns 5...
You NEED to allocate space for the terminating NULL, so do new
TCHAR[wordlen+1]. As is, you're getting an un-terminated word in your array
and seeing garbage that's past the end of your allocation. Note that in
general, new will allocate more memory than you ask for due to
alignment/granularity requirements.
btw, all of this is much easier and less error-prone if you use std::string
instead of dealing with low-level details yourself:
typedef std::basic_string<TCHAR> tstring;
tstring mystring("quick brown fox jumps over lazy dog");
tstring alphabet("abcdefgh...wxyz");
tstring word = mystring.substr(0,mystring.find_first_not_of(alphabet,0));
songie said:It returns 5...
yes, sorry 5. I meant 5.
You NEED to allocate space for the terminating NULL, so do new
TCHAR[wordlen+1]. As is, you're getting an un-terminated word in
your array and seeing garbage that's past the end of your
allocation. Note that in general, new will allocate more memory
than you ask for due to alignment/granularity requirements.
OK so supposing I do, then I'll get even more memory. But I see the
point, it *might* fill it. I'll put it another way...
no I won't, I'll just repeat the question. How can I get the variable
to contain JUST
the string, with terminating null if that's what it entails, and then
still successfully delete[] it?
Nah, that'd defeat the point of writing this part of the program in
unmanaged
C++. I might aswell write it in C#, that the rest of the program's
written in.
This is a routine that's going to be called probably every time
the user types a key, possibly many times per the user types a key.
mmm. wonder where find_first_not_of() comes from , the tooth fairy?
exactly as much space as you request - it's free to allocate more. That
said, any attempt by you to access beyond the size you requested is
undefined behavior. It's free to allocate exactly the amount you request
one time, and 10X the amount you request the next time - you simply cannot
assume anything beyond:
1. the allocation was at least as large as you requested.
2. you can safely access all of the elements that you requested (i.e. for
new T[n], you can access indexes 0..n-1).
3. assuming you haven't violated #2, that you can pass the same pointer
returned by new[] to delete[].
Then there's likely to valid reason to write it in unmanaged C++.
You're
apparently operating under the falacious assumption that managed code is
slow, or that this function is going to be a bottleneck in your program
(have you profiled it to find out?).
So? Users typing keys are monumentally slow - you can run 10's (maybe
100's) of millions of CPU instructions between keystrokes on a modern CPU.
Besides, it's likely that the std::string solution, if properly written,
will be the same speed as your fragile hand-crafted solution.
standard.find_first_not_of is a member function of std::basic_string<CharT> - note
the . between mystring and find_first_not_of in the above sample. It comes
not from the tooth fairy, nor from Microsoft, but from the ISO C++
songie said:oh ok, I stand corrected then. Although the STL is made by
hewlett-packard, you realise. But under the hood it's still probably
a similar algorithm to _tcscspn, and is just another header file of
mainly unnecessary
bumph compiled into the application.