[英]What is the byte alignment of the elements in a std::vector<char>?
I'm hoping that the elements are 1 byte aligned and similarly that a std::vector<int>
is 4 byte aligned ( or whatever size int
happens to be on a particular platform ). 我希望元素是1字节对齐的,类似地,
std::vector<int>
是4字节对齐的(或者特定平台上的任何大小的int
)。
Does anyone know how standard library containers get aligned? 有谁知道标准库容器如何对齐?
The elements of the container have at least the alignment required for them in that implementation: if int
is 4-aligned in your implementation, then each element of a vector<int>
is an int
and therefore is 4-aligned. 在该实现中,容器的元素至少具有它们所需的对齐:如果
int
在您的实现中是4对齐的,那么vector<int>
每个元素都是int
,因此是4对齐的。 I say "if" because there's a difference between size and alignment requirements - just because int
has size 4 doesn't necessarily mean that it must be 4-aligned, as far as the standard is concerned. 我说“if”因为大小和对齐要求之间存在差异 - 只是因为
int
大小4并不一定意味着它必须是4对齐的,就标准而言。 It's very common, though, since int
is usually the word size of the machine, and most machines have advantages for memory access on word boundaries. 但是,这很常见,因为
int
通常是机器的字大小,并且大多数机器都具有字边界内存访问的优势。 So it makes sense to align int
even if it's not strictly necessary. 因此,即使它不是绝对必要的,也可以对齐
int
。 On x86, for example, you can do unaligned word-sized memory access, but it's slower than aligned. 例如,在x86上,您可以执行未对齐的字大小的内存访问,但它比对齐慢。 On ARM unaligned word operations are not allowed, and typically crash.
在ARM上,不允许使用未对齐的字操作,并且通常会崩溃。
vector
guarantees contiguous storage, so there won't be any "padding" in between the first and second element of a vector<char>
, if that's what you're concerned about. vector
保证连续存储,因此在vector<char>
的第一个和第二个元素之间不会有任何“填充”,如果这是你所关心的。 The specific requirement for std::vector
is that for 0 < n < vec.size()
, &vec[n] == &vec[0] + n
. std::vector
的具体要求是0 < n < vec.size()
, &vec[n] == &vec[0] + n
。
[Edit: this bit is now irrelevant, the questioner has disambiguated: The container itself will usually have whatever alignment is required for a pointer, regardless of what the value_type is. [编辑:此位现在无关紧要,提问者已消除歧义:无论value_type是什么,容器本身通常都会有指针所需的对齐方式。 That's because the vector itself would not normally incorporate any elements, but will have a pointer to some dynamically-allocated memory with the elements in that.
这是因为向量本身通常不会包含任何元素,但是会有一个指针指向一些动态分配的内存以及其中的元素。 This isn't explicitly required, but it's a predictable implementation detail.]
这不是明确要求的,但它是一个可预测的实现细节。]
Every object in C++ is 1-aligned, the only things that aren't are bitfields, and the elements of the borderline-crazy special case that is vector<bool>
. C ++中的每个对象都是1对齐的,唯一没有的是bitfields,而borderline-crazy特殊情况的元素是
vector<bool>
。 So you can rest assured that your hope for std::vector<char>
is well-founded. 所以你可以放心,你对
std::vector<char>
希望是有根据的。 Both the vector and its first element will probably also be 4-aligned ;-) 向量及其第一个元素也可能是4对齐的;-)
As for how they get aligned - the same way anything in C++ gets aligned. 至于它们如何对齐 - 就像C ++中的任何内容一致。 When memory is allocated from the heap, it is required to be aligned sufficiently for any object that can fit into the allocation.
从堆中分配内存时,需要对任何适合分配的对象进行充分对齐。 When objects are placed on the stack, the compiler is responsible for designing the stack layout.
当对象放在堆栈上时,编译器负责设计堆栈布局。 The calling convention will specify the alignment of the stack pointer on function entry, then the compiler knows the size and alignment requirement of each object it lays down, so it knows whether the stack needs any padding to bring the next object to the correct alignment.
调用约定将指定堆栈指针在函数入口上的对齐,然后编译器知道它放下的每个对象的大小和对齐要求,因此它知道堆栈是否需要任何填充以使下一个对象进入正确的对齐。
I'm hoping that the elements are 1 byte aligned and similarly that a std::vector is 4 byte aligned ( or whatever size int happens to be on a particular platform ).
我希望这些元素是1字节对齐的,并且类似于std :: vector是4字节对齐的(或者任何大小的int恰好在特定平台上)。
To put it simply, std::vector
is a wrapper for a C array. 简单地说,
std::vector
是C数组的包装器。 The elements of the vector are aligned as if they were in the array: elements are guaranteed to occupy continues memory block without any added gaps/etc, so that std::vector<N> v
can be accessed as a C array using the &v[0]
. 向量的元素对齐就好像它们在数组中一样:元素保证占用连续的内存块而没有任何添加的间隙/ etc,因此
std::vector<N> v
可以使用&v[0]
作为C数组访问&v[0]
。 (Why vector has to reallocate storage sometimes when elements are added to it.) (为什么vector有时会在添加元素时重新分配存储空间。)
Does anyone know how standard library containers get aligned?
有谁知道标准库容器如何对齐?
Alignment of elements is platform specific but generally a simple variable is aligned so that its address is divisible by its size (natural alignment). 元素的对齐是特定于平台的,但通常对齐一个简单的变量,使其地址可以被其大小(自然对齐)整除。 Structures/etc are padded (empty filler space at the end) on the largest data type they contain to ensure that if the structure is put into an array, all fields would retain their natural alignment.
结构/等在它们包含的最大数据类型上填充(末端的空填充空间),以确保如果将结构放入数组中,则所有字段将保持其自然对齐。
For other containers (like std::list
or std::map
) use data via template mechanics are made a part of internal structure and the structure is allocated by operator new
. 对于其他容器(如
std::list
或std::map
),通过模板机制使用数据成为内部结构的一部分,结构由operator new
分配。 The new
is guaranteed (custom implementation must obey the rule too; inherited from the malloc()
) to return memory block which is aligned on largest available primitive data type (*). new
保证(自定义实现也必须遵守规则;继承自malloc()
)以返回在最大可用原始数据类型(*)上对齐的内存块。 That is to ensure that regardless what structure or variable would be places in the memory block, it will be accessed in aligned fashion. 这是为了确保无论内存块中的哪个结构或变量都是位置,它将以对齐的方式访问。 Unlike
std::vector
, obviously, the elements of most other STL containers are not guaranteed to be within the same continuous memory block: they are new
ed one by one, not with new[]
. 与
std::vector
不同,显然,大多数其他STL容器的元素不能保证在同一个连续内存块中:它们是逐个new
编辑的,而不是new[]
。
(*) As per C++ standard, "The allocation function (basic.stc.dynamic.allocation) called by a new-expression (expr.new) to allocate size bytes of storage suitably aligned to represent any object of that size." (*)根据C ++标准,“new-expression(expr.new)调用的分配函数(basic.stc.dynamic.allocation)分配大小的存储字节,适当地对齐以表示该大小的任何对象。” That is a softer requirement compared to one
malloc()
generally abides, as per POSIX: "The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object [...]". 与一个
malloc()
通常相比,这是一个更软的要求,根据POSIX:“如果分配成功,返回的指针应该适当地对齐,以便它可以被分配给指向任何类型对象的指针[...]” 。 C++ requirement in a way reenforces the natural alignment requirement: dynamically allocated char
would be aligned as char
requires, but not more. C ++需求在某种程度上重新满足了自然对齐要求:动态分配的
char
将按char
需要对齐,但不能更多。
Do you mean the vector members, or the vector structure itself? 你的意思是矢量成员,还是矢量结构本身? Members are guaranteed to be contiguous in memory but structure alignment is platform/compiler-dependent.
保证成员在内存中是连续的,但结构对齐依赖于平台/编译器。 On Windows this can be set at compile-time and also overridden using
#pragma pack()
. 在Windows上,这可以在编译时设置,也可以使用
#pragma pack()
覆盖。
The answer for other containers is likely not the same as for vector, so I would ask specific questions about the ones you care about. 其他容器的答案可能与vector不同,所以我会询问有关你关注的容器的具体问题。
The alignment of the whole container is implementation dependent. 整个容器的对齐取决于实现。 It is usually at least sizeof(void*), that is 4 or 8 bytes depending on the platform, but may be larger.
它通常至少是sizeof(void *),取决于平台,为4或8个字节,但可能更大。
If special (guaranteed) alignment is needed use plain arrays or write/adapt some generic array class with: 如果需要特殊(保证)对齐,请使用普通数组或使用以下方法编写/调整某些通用数组类:
// allocation
char* pointer = _mm_malloc(size, alignment);
// deallocation
_mm_free(pointer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.