数组向量的内存布局是什么? [英] What is the memory layout of a vector of arrays?
问题描述
类型为Vec<[f3; 5]>
的变量是作为一个连续数组(为Vec::len() * 5 * sizeof(f32)
个字节)存储还是作为指针的Vec
存储?
Are variables of type Vec<[f3; 5]>
stored as one contiguous array (of Vec::len() * 5 * sizeof(f32)
bytes) or is it stored as a Vec
of pointers?
推荐答案
Vec<T>
的内容与self.capacity() * std::mem::size_of::<T>()
字节一样,是单个堆分配(与T
无关). (Vec
求和,这是Vec<T>
的全部要点,而不是Box<[T]>
,因此在此计算中,容量而不是长度是要紧的.)实际的Vec<T>
本身需要三个词(
The contents of a Vec<T>
is, regardless of T
, a single heap allocation, of self.capacity() * std::mem::size_of::<T>()
bytes. (Vec
overallocates—that’s the whole point of Vec<T>
instead of Box<[T]>
—so it’s the capacity, not the length, that matter in this calculation.) The actual Vec<T>
itself takes three words (24 bytes on a 64-bit machine).
[f32; 5]
只是一块内存,其中包含五个32位浮点数,没有间接寻址;这是二十个字节(因此 std::mem::size_of::<[f32; 5]>() == 20
).
[f32; 5]
is just a chunk of memory containing five 32-bit floating-point numbers, with no indirection; this comes to twenty bytes (hence std::mem::size_of::<[f32; 5]>() == 20
).
这篇关于数组向量的内存布局是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!