问题
I get that using negative indexes is just pure luck. But out of curiousity I tried this. I know you can declare array[0]; just like malloc(0); is legal. But how come I can store a value in array[0]?
#include <stdio.h>
#include <conio.h>
int main(void)
{
int i;
int array[0];
array[0] = 5;
printf("%d\n",array[0]);
getch();
}
回答1:
Such a 0
sized array is a constraint violation in standard C, you compiler should not let you get away with this without giving you a diagnostic. If it doesn't tell you something then, that must be an extension that your compiler vendor has added to its C dialect.
Don't rely on such extensions.
Regardless whether or not you'd declare the array with size 0
, C has no imposed bound checking of array or pointer access. A good modern compiler should still give you a warning, though, if the excess of the bounds is known at compile time.
回答2:
You are accessing a memory space that is either undefined or used by something else. This code will eventually screw something up because until you use malloc you have no idea where the array exists. You created an array of size zero. C will let you write whatever you want to wherever you give it an address. Barring the operating system from interfering. "array" is a pointer to an array and it has some arbitrary value. The compiler has no idea that that memory space should be reserved for the zero'th element of array as you are using it.
回答3:
C assumes (where it can) that you know what you're doing. A zero length array still has an address (but that address can easily be shared with something else of size). When you index into that array, you're just modifying the memory location you're using, without concern for what else uses the address you end up with -- and by writing, you can easily cause huge (and difficult to debug) problems.
回答4:
Just like Vikdor said in the comment, you are writing in a memory location not reserved for you. This might result in serious and hard to debug problems so I suggest you never do this, but this is how it works:
when you declare an array of size 0 int array[0]
the complier is going to associate the name 'array' with a memory location. Let's say 100 for this example. But because the size of the array is 0, no byte belongs to the array, so bytes 100, 101 and so on might be allocated to other variables too.
When you say array[0] = 5
you are writing the number 5 to bytes 100, 101, 102 and 103, because an int is 4 bytes long.
And then you can read that number using array[0] that reads 4 bytes starting at location 100.
A problem appears when the space starting at 100 is given to some other variable because then it might overwrite array[0] and it will appear as if array[0] had changed for no reason (this is where you will spend lots of frustrating time debugging)
Keep in mind that int array[0] is just like int *array. The name of an array is just a pointer.
回答5:
int array[0];
is Invalid C code.It is non-standard conforming. Your code compiles cleanly because it uses a compiler specific extension.
Reference:
C99 Standard: 6.7.5.2 Array declarators
Para 1:
In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or
*
. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. The element type shall not be an incomplete or function type. The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.
Why does the seem to work?
Assuming your compiler implementation allows zero length array's:
array[0] = 5;
still works because this code statement causes Undefined Behavior.
It writes to a memory region that is not owned by the array thus overwritting the bounds of allocated memory. Luckily, it works because the memory is probably not in use by some other entity. Technically, it is still an Undefined Behavior.
来源:https://stackoverflow.com/questions/12620531/declaring-an-array-with-0-number-of-elements-can-still-store-values