1

I'm trying to convert a wstring to const wchar*, but the null terminations aren't working as I expect:

index 4

index 5

code

Is there any way to convert wstring to const wchar* in the manner that I'm trying?

user2002287
  • 51
  • 1
  • 6
  • possible duplicate of [I want to convert std::string into a const wchar\_t \*](http://stackoverflow.com/questions/246806/i-want-to-convert-stdstring-into-a-const-wchar-t) – 101010 May 14 '14 at 21:53
  • have you tried const_cast? – Beed May 14 '14 at 21:54
  • @Beed, `c_str()` returns `const wchar_t *`. It's stored into a `const wchar_t *`. Why would a `const_cast` help? – chris May 14 '14 at 21:55
  • I don't see the problem here? The type of your pointer is `const wchar_t*`, the debugger advances by *2* bytes on pointer increment. Which means the `+5` case is past the null character – indeterminately sequenced May 14 '14 at 21:56
  • The stuff stored at `works + 5` is something you promised never to look at when you used the language. Same with `doesntWork + 5`. Don't be surprised when you implicitly say you won't look there and then look there and don't get what you expect. – chris May 14 '14 at 21:59
  • What problems are you experiencing? Because the statements are OK. In which context `doesntWork` doesn't work? – 101010 May 14 '14 at 21:59
  • I look into my crystal ball and I saw that you're using `std::cout` instead of `std::wcout` to print `doesntwork`. – 101010 May 14 '14 at 22:04
  • @40two That is the code that I'm currently using, but it isn't solving my problem. – user2002287 May 14 '14 at 22:04
  • @user2002287 what's your problem? The conversion looks OK. – 101010 May 14 '14 at 22:05
  • @40two I'm not using any form of cout, I'm passing the strings into client code. When I use the doesntWork it throws an exception, saying the string formatting is incorrect. work has a null terminator which inhabits +4 and +5, doesntWork's null terminator only inhabits +4 (i think?) I've been working on this ALL day. I believe it has something to do with "works" bring a wide string, and "doesntWork" not having a wide null terminator? – user2002287 May 14 '14 at 22:08
  • 1
    @user2002287, `works`'s null terminator is just `works[4]`. Each element is one `wchar_t`. The null is `sizeof(wchar_t)` zeroed bytes in that case. – chris May 14 '14 at 22:08
  • @40two I'm passing both strings into the same function. works works. doesntWork throws a wrong string formatting exception – user2002287 May 14 '14 at 22:09
  • 1
    @user2002287 are you passing to a function that expects a BSTR ? – M.M May 14 '14 at 22:10
  • @chris then I have no idea how they are different. – user2002287 May 14 '14 at 22:10
  • 1
    @user2002287 bear in mind that the result of `c_str()` is only valid until the next time you call a non-const member function of the string, or the string is destroyed – M.M May 14 '14 at 22:10
  • @user2002287 Then your client code is engaging in undefined behavior ( something like 40two's `cout< – indeterminately sequenced May 14 '14 at 22:13

1 Answers1

3

Both works[5] and doesntWork[5] cause undefined behavior. The buffers are only valid up to [4], and [5] is outside that.

const wchar_t *works = L"Test";

works[0] -> L'T'
works[1] -> L'e'
works[2] -> L's'
works[3] -> L't'
works[4] -> L'\0'
works[5] -> undefined behavior, the program is broken if it ever tries to access this
bames53
  • 86,085
  • 15
  • 179
  • 244