1
 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
  • 383
  • 1
  • 7
  • 11
  • 3
    [That is not valid C++](http://stackoverflow.com/questions/312116/c-array-size-dependent-on-function-parameter-causes-compile-errors) – DCoder Jan 06 '13 at 20:51
  • http://www.zetcode.com/gui/winapi/controls/ – Gladstone Asder Jan 06 '13 at 20:53
  • so the guy who made the tutorial made a c program and not a c++ program? – Gladstone Asder Jan 06 '13 at 20:53
  • oh damn it's a c tutorial lol, thanks google – Gladstone Asder Jan 06 '13 at 20:54
  • 3
    "*This is Windows API tutorial for the **C** programming language. <...> Note that this tutorial uses **C99**.*" – DCoder Jan 06 '13 at 20:54
  • 1
    gcc also provides this in C++ as an extension. – Jerry Coffin Jan 06 '13 at 20:55
  • dynamic sizes of statically allocated arrays are fine in C99, but not in C++, where you don't really need it since you have std::vector. – gustaf r Jan 06 '13 at 20:56
  • 1
    This is the important part: **expected constant expression**. – Bo Persson Jan 06 '13 at 21:22
  • Just to say what everyone else has already said years ago without saying: `GetWindowTextLengthW(hwndEdit)` is not null + 1 = 1. That would be like saying, infinity plus one equals one. The compiler doesn't know what is going to be returned and thus cannot guess to prepare enough space. Variable Length Array(VLA)s were added in C99 whether or not compilers supported them; and subsequently made optional in C11. VisualStudio 10 did not implement VLAs and was non conforming to C99 and it did not need to conform by C11 standards as such things were made optional. Probably not an issue any longer. – haelmic Feb 19 '23 at 02:15

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,224
  • 9
  • 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
  • 124,023
  • 23
  • 387
  • 451
  • 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,894
  • 12
  • 69
  • 85
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,550
  • 2
  • 30
  • 53