0
 int len = GetWindowTextLengthW(hwndEdit) + 1;
 wchar_t text[len];

I get

Error 2 error C2466: cannot allocate an array of constant size 0
Error 3 error C2133: 'text' : unknown size
Error 1 error C2057: expected constant expression

I don't understand why it wont compile, because GetWindowTextLengthW(hwndEdit) + 1 > 0

Isn't it true that null+1 = 1?

Gladstone Asder
  • 363
  • 1
  • 7
  • 11

4 Answers4

5

What you want is not to have to care about memory management, right? That's why you chose a statically allocated array.

Yes, you can use new as the answers here recommend, I however recommend:

std::vector< wchar_t > text;

gustaf r
  • 1,174
  • 8
  • 14
3

First of all, you are using the syntax for declaring a statically sized array but you pass in a size variable which is evaluated at run-time. This is why it does not compile.

Second, you cannot allocate an array statically with a size of 0, but that's another issue (although std::array allows you doing that).

I think you should use dynamic allocation instead:

wchar_t* text = new wchar_t[len]

or even better, use std::wstring or std::vector<wchar_t >

Andy Prowl
  • 119,862
  • 22
  • 374
  • 446
  • 1
    Probably `std::vector` in this case. I imagine it's going to be used as the buffer for `GetWindowText`, and `std::wstring` can't do that directly. – chris Jan 06 '13 at 20:53
  • 2
    @You can do that with `std::wstring` as well, as in: `std::wstring text(len, 0); GetWindowText(&text[0]);`. In theory that was allowed to fail in C++98/03 (but it never really did). In C++11, std::basic_string requires a contiguous buffer. – Jerry Coffin Jan 06 '13 at 20:57
  • @JerryCoffin, Oh, that *is* valid in C++11? Well that actually helps a whole lot, thanks! – chris Jan 06 '13 at 20:58
  • @chris: I don't know of anything that directly states that you can write to the buffer, but it definitely requires a contiguous buffer and says nothing about writing to it causing a problem. In practice, I can't see how it'd be a problem as long as you don't overflow the buffer or something like that. – Jerry Coffin Jan 06 '13 at 21:00
  • @JerryCoffin: thanks for contributing. I edited my answer according to your comment – Andy Prowl Jan 06 '13 at 21:02
  • @JerryCoffin, That would seem to be the case. I never really came to realize that the contiguous storage requirement had an effect to this extent. Of course the problems you mention are also applicable to `std::vector` or any other buffer you use, not specific to `std::string`. – chris Jan 06 '13 at 21:08
0

Try:

wchar_t* text = new wchar_t[len];
Andrew
  • 11,760
  • 12
  • 66
  • 83
0

It is true that an error message that complains about a zero instead of a non-constant value is confusing (just like some compilers complain about int for undefined types). VLA are a C99 feature, only present as an extension in some C++ compilers (and on its way to be partially added to C++14 under a different name). The closest equivalent (allocate on the stack, no call to a deallocation function) under MSVC is _alloca.

Marc Glisse
  • 7,002
  • 2
  • 26
  • 51