Setting array size with a variable - What does the C compiler do?


Joined
Feb 25, 2022
Messages
2
Reaction score
0
I ran across some code that I think has an error, but I've been unable to find out what C is supposed to do with this. There are two source code statements that look like this:
long buffer_bit_length = 0;
unsigned char buffer[(buffer_bit_length/8)];
I was surprised this actually compiled, since the buffer is being defined with a length that is a variable. I believe C resolves this array definition at compile time, not at run time, so it must resolve "buffer_bit_length/8" as some kind of constant that the array definition uses - but what does it resolve to, and is all of this well-defined in the C language? I couldn't find anything that describes it.

Later on in this code, data is copied to the buffer using sizeof(buffer) as the length. I put a printf at that point to see what sizeof(buffer) was, and it turns out it's zero - so the array declaration probably allocated a zero-length array. That would mean that any attempt to put data in the array would probably overwrite memory allocated for something else.

Does anyone know what the compiler is doing for this case, and if it's actually defined in such a way that all C compilers would do the same thing?

(Personally, I plan to contact the author of the code and suggest writing it a different way.)
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
31
Nah, that's legit. It's variable-length array. Added in C99:

If expression is not an integer constant expression, the declarator is for an array of variable size.

Each time the flow of control passes over the declaration, expression is evaluated (and it must always evaluate to a value greater than zero), and the array is allocated...

"Expression" being what was declared in the square brackets. Then, further, with respect to sizeof:

If the type of expression is a variable-length array type, expression is evaluated and the size of the array it evaluates to is calculated at run time.

Reference: array, sizeof

Might feel dirty, but it's well-defined and good to go. What would be a better way to write that?
 
Joined
Feb 25, 2022
Messages
2
Reaction score
0
Interesting - I guess it shows that I wrote the majority of my C code before 1999 :). Regardless, there IS an error in the code I was looking at, because it uses sizeof(array) to decide how many bytes to copy into the array, and at that point the variable used to define the size of the array is still set to zero and sizeof(array) is zero. I'll have to let the owner know about that.
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
31
I was a little skeptical myself. I'm not sure how it's being calculated and if the size is being stored in some implicit, hidden variable somewhere. It does feel dirty. Run-time size stuff feels like it should require a heap-allocated array with the coder storing the size itself. Or C++ and a std::vector.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top