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