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 ).
Does anyone know how standard library containers get aligned?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The elements of the container have at least the alignment required for them in that implementation: if
intis 4-aligned in your implementation, then each element of avector<int>is anintand therefore is 4-aligned. I say “if” because there’s a difference between size and alignment requirements – just becauseinthas size 4 doesn’t necessarily mean that it must be 4-aligned, as far as the standard is concerned. It’s very common, though, sinceintis usually the word size of the machine, and most machines have advantages for memory access on word boundaries. So it makes sense to aligninteven if it’s not strictly necessary. On x86, for example, you can do unaligned word-sized memory access, but it’s slower than aligned. On ARM unaligned word operations are not allowed, and typically crash.vectorguarantees contiguous storage, so there won’t be any “padding” in between the first and second element of avector<char>, if that’s what you’re concerned about. The specific requirement forstd::vectoris that for0 < 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. 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>. So you can rest assured that your hope forstd::vector<char>is well-founded. Both the vector and its first element will probably also be 4-aligned 😉As for how they get aligned – the same way anything in C++ gets aligned. 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.