简体   繁体   English

如何在C函数内部定义数组?

[英]How to define an array inside a function in C?

So in my source file I have the folowin function: 因此,在我的源文件中,我具有folowin函数:

void update(state* old_state, state* measurement, uint32_t size)
{
  state new_state[size];
  //some function using measurement and old_state and returning the result in newstate

  arm_fadd_32(measurement,old_state,newstate,size);

// rest of the code } //代码的其余部分}

Now the compiler throws an error saying that error#28:expression must have a constant value. 现在,编译器将引发错误,指出error#28:expression必须具有恒定值。 I think it's due to the fact that even though inside the method the size local variable is not changing the compiler is expecting a constant while defining the size. 我认为这是由于这样的事实,即使在方法内部,大小局部变量没有改变,编译器在定义大小时也期望常量。 I have tried the following: 我尝试了以下方法:

int const a = size; 

and then tried to reinitialize it says constant value is not known. 然后尝试重新初始化,表示常量值未知。 I did some research in internet and it appears that there is no easier way without using malloc, which I don't want to since I am using the code for some embedded application. 我在Internet上进行了一些研究,似乎不使用malloc没有比这更简单的方法了,因为我将代码用于某些嵌入式应用程序,所以我不希望这样做。

Is there a way to avoid this problem without really using malloc? 有没有一种方法可以避免这个问题而无需真正使用malloc? Thanks in advance guys! 在此先感谢大家!

No, not really. 不,不是。 If the compiler doesn't allow variable length arrays, it means it expects a compile-time constant . 如果编译器不允许使用可变长度数组,则意味着它需要一个编译时常量

VLAs are supported in C99, which is probably not what you're using. C99支持VLA,这可能不是您所使用的。

int const a declares a constant variable (haha!), but it's by no means a compile-time constant. int const a声明一个常量变量(haha!),但绝不是编译时常量。 You'll need to use malloc . 您需要使用malloc

As of the 1990 ISO C standard, the size of an array must be a constant integer expression. 从1990 ISO C标准开始,数组的大小必须为常数整数表达式。 Note "constant", not const . 注意“ constant”,而不是const A constant expression is (roughly) one whose value is known at compile time; 常量表达式(大约)是在编译时就知道值的表达式。 const , though it comes from the same root word, really just means "read-only". const尽管来自同一个词根,但实际上仅表示“只读”。

The 1999 standard added VLAs (variable length arrays), which would make your code legal. 1999年标准添加了VLA(可变长度数组),这将使您的代码合法。 One drawback of VLAs is that there's no mechanism to detect a failure to allocate the required space; VLA的一个缺点是没有机制来检测分配所需空间的失败。 if the allocation fails, your programs behavior is undefined. 如果分配失败,则您的程序行为未定义。 (It may crash if you're lucky .) 如果幸运的话,它可能会崩溃。)

Most C compilers these days do support most of the C99 standard; 如今,大多数C编译器确实支持大多数C99标准。 you may need a command-line option to enable it. 您可能需要命令行选项才能启用它。 Microsoft's C compiler, on the other hand, supports only C90, plus a very few C99-specific features, and Microsoft has stated that they have no plans to change that. 另一方面,Microsoft的C编译器仅支持C90,以及极少数的C99特定功能,并且Microsoft表示他们没有计划对此进行更改。 If you'll lets us know what compiler you're using, we can probably be more helpful. 如果您让我们知道您使用的是什么编译器,我们可能会更有帮助。

You can use malloc() to allocate a dynamically sized array on the heap: 您可以使用malloc()在堆上分配动态大小的数组:

state *new_state = malloc(size * sizeof *new_state);
if (new_state == NULL) {
    // allocation failed, handle the error somehow
}

Note that malloc() returns a null pointer to indicate an allocation failure, which is an advantage over VLAs even if your compiler supports them. 请注意, malloc()返回一个空指针以指示分配失败,即使您的编译器支持VLA,这也是VLA的优势。

Your C compiler clearly does not support C99 VLAs. 您的C编译器显然不支持C99 VLA。 Which means that arrays have to have dimensions known at compile time. 这意味着数组在编译时必须具有已知的维数。 And yours does not. 而你没有。

Obviously you can allocate on the heap with malloc . 显然,您可以使用malloc在堆上进行分配。 But you have stated that you don't want to do that for performance reasons. 但是您已经说过,出于性能原因,您不想这样做。 If you really must have stack allocated memory, whose size is determined at runtime, then you need to use alloca . 如果确实必须有堆栈分配的内存(其大小是在运行时确定的),则需要使用alloca

Beware that alloca is fraught with danger. 当心alloca充满危险。 Stack Overflow is an ever-present danger when using alloca , just as it is when using a C99 VLA. 与使用C99 VLA时一样,使用alloca ,堆栈溢出是永远存在的危险。

In C89 array size must be constant. 在C89中,数组大小必须恒定。 The const qualifier does not qualify an object to be constant but rather to be read-only. const限定符不会将对象限定为常数 ,而是变为只读。

In C99 you can have non constant array size and these arrays are called variable length arrays (VLA). 在C99中,您可以使用非恒定的数组大小,这些数组称为可变长度数组(VLA)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM