What is the memory structure of an array vector?

Are variables of type Vec<[f3; 5]> Vec<[f3; 5]> saved as one continuous array (from Vec::len() * 5 * sizeof(f32) bytes) or stored as Vec pointers?

+6
source share
1 answer

The contents of Vec<T> , regardless of T , are a single heap distribution, self.capacity() * std::mem::size_of::<T>() bytes. ( Vec generalocates is the whole point of Vec<T> instead of Box<[T]> , as well as its capacity, not the length, which matters in this calculation.) The actual Vec<T> itself takes three words ( 24 bytes of 64 beat machine).

[f32; 5] [f32; 5] - this is just a piece of memory containing five 32-bit floating-point numbers, without indirection; this is twenty bytes (hence std::mem::size_of::<[f32; 5]>() == 20 ).

+8
source

All Articles