unsigned int data_size = 32;
unsigned char data[data_size];
unsigned int sense_len = 256;
unsigned char sense[sense_len];
It is not a bad idea at all since you don't have to clean it up afterwards / use dynamic memory allocation.
#define data_SIZE xxxx
data_t data[data_SIZE];
size_t data_size = data_SIZE;
It is not a bad idea at all since you don't have to clean it up afterwards / use dynamic memory allocation.
I prefer this approachCode: [Select]#define data_SIZE xxxx
data_t data[data_SIZE];
size_t data_size = data_SIZE;
this way, it works on all my C compilers, including the old one used by IDT for their MIPSR2K prototypes (~1995).
void fun(int n)
{
int arr[n]; // <--------- the ICE here thinks WTF?!? is the size of arr?!?
// ......
}
int main()
{
fun(6);
}

You know that you could have made the size initializer a constant ?

const unsigned int data_size = 32;
unsigned char data[data_size];
const unsigned int sense_len = 256;
unsigned char sense[sense_len];
Now it will.
C is a bit more limited than C++ with respect to dimensioning arrays.
You can create a VLA ( Variable-length array ) with dynamic size, or you can create
an array of either zero (special cases) or fixed dimensions.
It is perhaps unfortunate that one can't use an expression which involves a static or global const variable whose constant initializer value is known to the compiler at the time of the array definition to also dimension the array by a shared indirect "constant" expression value.
In C++ one can use constexpr values as array dimensions more freely than the plain "const" values which are comparable in both C and C++.
VLAs consume stack space
)

const unsigned int data_size = 32;
unsigned char data[data_size];
const unsigned int sense_len = 256;
unsigned char sense[sense_len];
Or: #define data_size 32
unsigned char data[data_size];
#define sense_len 256
unsigned char sense[sense_len];
Although you're initializating the variable, it's still a ram variable, could change anytime, that's what the compiler sees, while value of a variable declaration must be constant.
Options are this:Code: [Select]const unsigned int data_size = 32;
unsigned char data[data_size];
const unsigned int sense_len = 256;
unsigned char sense[sense_len];
It appears some compilers behave non-standard when given an const integer as array declarator expression.
)
Have a look at the below link;
it fails with an error on gcc, though clang passes it with a warning that it allowed it as an extension:
https://godbolt.org/z/3fPhnhYTz
That is a handy site because one can easily try most common compilers / platforms / compilation options and see what
a snipped of code compiles to or if it compiles without errors / warnings at all.
That's for a static storage version of your suggested 'const' code.
If the storage wasn't specified to be static then the 'const' wouldn't matter since
one can create VLAs even with dynamic lengths no problem so of course a const dimension would also have worked
to create a VLA. So if that's the aim then no problem. But it isn't a general solution for creating
truly 'const' global / extern / static data arrays of constant dimension and initialization such as one
could commonly have linked to be placed in FLASH / ROM / RODATA etc. instead of living on the RAM stack.
...
"const" is banned from my C-coding style, and I tend to use the linker_script to specifically allocate constant variables in the RO session, hence again, I don't need "const".
...
Why? Size is declared as const. The compiler knows that.
It compiles right away without any warning.
In the instant you remove const - error!
But just see for yourself looking at assembly code and trying various code combinations and optimization levels.
I rewrote the whole C source in a clean way.
No more "const",

I think you misspelled "less safe"