Why doesn't the fixed unmanaged pointer work the same?
That's a strange question. Why do you believe it should?
The contract is: when you fix an array with n elements where n > 0 you get a pointer to a buffer from which you can read and write n elements.
Now, When n is zero, null is a pointer to a buffer from which you can read and write zero elements, so as it turns out, that contract is actually met for the case where n is zero. The C# language is not required to do so. The specification says
The behavior of the fixed statement is implementation-defined if the array expression is null or if the array has zero elements.
So the implementation would be entirely within its rights to, say, throw an exception in your program. The meaning of your program is actually not defined at all by the C# language specification.
You're trying to use fixed
off-label in order to do something incredibly dangerous and wrong. Don't do that. You should use fixed
on an array for one thing only: to obtain a pointer to a buffer of n elements that you can read and write.
Is there a way I could get, in code, the actual address of the empty array?
Yes. Pin manually with a GCHandle
.
Pinning a managed object in order to obtain its address is almost always dangerous and wrong.
I need that to be able to get to the array's header, to modify the array's runtime-type on the fly.
That's always dangerous and wrong. Don't do that under any circumstances.