不能分配一个常数大小为0的数组。

时间:2022-09-06 12:14:04
 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

错误2错误C2466:不能分配一个常数大小的0错误3错误C2133:“文本”:未知大小错误1错误C2057:期望常数表达式。

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

我不明白为什么它不能编译,因为GetWindowTextLengthW(hwndEdit) + 1 > 0。

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

null+1 = 1不是真的吗?

4 个解决方案

#1


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:

是的,你可以用new作为答案,我建议:

std::vector< wchar_t > text;

std::向量< wchar_t >文本;

#2


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).

其次,您不能静态地分配一个大小为0的数组,但这是另一个问题(尽管std::array允许您这样做)。

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 >

或者更好,使用std::wstring或std::vector

#3


0  

Try:

试一试:

wchar_t* text = new wchar_t[len];

#4


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.

确实,一个错误消息抱怨一个0而不是一个非常量值是令人困惑的(就像一些编译器抱怨int为未定义类型)。VLA是C99特性,只是在一些c++编译器中作为一个扩展(在不同的名称下,它被部分地添加到c++ 14中)。在MSVC下,最接近的等价(在堆栈上分配,没有调用deallocation函数)是_alloca。

#1


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:

是的,你可以用new作为答案,我建议:

std::vector< wchar_t > text;

std::向量< wchar_t >文本;

#2


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).

其次,您不能静态地分配一个大小为0的数组,但这是另一个问题(尽管std::array允许您这样做)。

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 >

或者更好,使用std::wstring或std::vector

#3


0  

Try:

试一试:

wchar_t* text = new wchar_t[len];

#4


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.

确实,一个错误消息抱怨一个0而不是一个非常量值是令人困惑的(就像一些编译器抱怨int为未定义类型)。VLA是C99特性,只是在一些c++编译器中作为一个扩展(在不同的名称下,它被部分地添加到c++ 14中)。在MSVC下,最接近的等价(在堆栈上分配,没有调用deallocation函数)是_alloca。