简体   繁体   中英

How to define an array inside a function in C?

So in my source file I have the folowin function:

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

Is there a way to avoid this problem without really using 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.

int const a declares a constant variable (haha!), but it's by no means a compile-time constant. You'll need to use malloc .

As of the 1990 ISO C standard, the size of an array must be a constant integer expression. Note "constant", not 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".

The 1999 standard added VLAs (variable length arrays), which would make your code legal. One drawback of VLAs is that there's no mechanism to detect a failure to allocate the required space; 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; 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. 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:

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.

Your C compiler clearly does not support C99 VLAs. 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 . 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 .

Beware that alloca is fraught with danger. Stack Overflow is an ever-present danger when using alloca , just as it is when using a C99 VLA.

In C89 array size must be constant. The const qualifier does not qualify an object to be constant but rather to be read-only.

In C99 you can have non constant array size and these arrays are called variable length arrays (VLA).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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