Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an array of size 0 consumes value, forgetting it

Tags:

rust

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?

like image 890
Filipe Rodrigues Avatar asked Jul 26 '20 19:07

Filipe Rodrigues


People also ask

Can you create an array of size 0?

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 .

How do you declare the size of an array in runtime?

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.

Can we create an array at runtime in C?

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.

Which of the following syntaxes would you use to determine the size of any array?

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.

Is it possible to create an array with a size 0?

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

What is a zero-sized array in C?

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.

How to allocate an array with a constant value greater than zero?

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 [].

How is memory allocated for an array with a length zero?

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.


Video Answer


1 Answers

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.

like image 131
programmerjake Avatar answered Oct 26 '22 16:10

programmerjake