I stumbled upon this question, and having read ramslök's comment on the accepted answer, I tried it out with a type that was not Copy
.
To my surprise it compiled successfully, but it's destructor never ran, effectively forgetting the value, as if std::mem::forget
was called. Here is a code example (Playground):
#[derive(PartialEq, Debug)]
struct A;
impl Drop for A {
fn drop(&mut self) {
println!("Dropping A");
}
}
fn main() {
let vec: Vec<A> = vec![];
let a = A;
assert_eq!(vec, [a; 0]);
}
If we tried to use a
after the assert_eq
, it complains the value was moved into the array.
Forgetting values is already possible via the std::mem::forget
, and it does not cause U.B., but it still feels weird that it's possible without compiler magic.
My question is: Is this a bug, or is it an intended feature of rust?
java , a Java array of size 0 can be created but it will be of no use because it cannot contain anything. To print the size of emptyArray in above program we use emptyArray. length that returns the total size, zero of course, of emptyArray .
If you need to initialize the data, you can use calloc: int* arr = calloc (nb_elems, sizeof(int)); /* Do something with your array, then don't forget to release the memory */ free (arr); This way, the allocated memory will be initialized with zeroes, which can be useful.
An array can also be initialized at runtime using scanf() function. This approach is usually used for initializing large array, or to initialize array with user specified values.
To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.
Yes its possible to create a size 0 array of any data type . An Array will get memory , either when it’s intialzed with array elements or size of an array is mentioned using new keyword (size should not be zero).
However, there are two situations in C that can be described as “zero-sized arrays”, they are flexible array member and dynamic allocation of size zero. Flexible array member is an array of unknown (not zero) size that appears as the last element of a struct that has at least one other member.
If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero. However, you can dynamically allocate an array of zero length with new [].
An array instance with the given dimension lengths is allocated. If there is not enough memory available to allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are executed. So for a length of zero, memory is allocated.
It is a bug, [a; 0]
is intended to evaluate and drop a
, as described in Rust's docs for the array type:
Note that [expr; 0] is allowed, and produces an empty array. This will still evaluate expr, however, and immediately drop the resulting value, so be mindful of side effects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With