What is element byte alignment in std :: vector <char>?

I hope that the elements will be aligned by 1 byte and in a similar way that std::vector<int>will be aligned by 4 bytes (or any size intwill be on a specific platform).

Does anyone know how standard library libraries are aligned?

+5
source share
5 answers

Container elements have at least the alignment required for them in this implementation: if it intis 4-aligned in your implementation, then each element vector<int>is intand, therefore, is 4-aligned. I say “if” because there is a difference between the size and alignment requirements - just because it intis 4 in size does not necessarily mean that it should be aligned in four, as far as the standard is concerned. This is very common, though, since intit is usually the word size of a machine, and most machines have advantages for accessing memory at word boundaries. Therefore, it makes sense to aligninteven if it is not strictly necessary. For example, on x86, you can perform non-equilibrium memory access by the size of a word, but it is slower than aligned. In ARM, non-primary word operations are not allowed and usually fail.

vectorguarantees continuous storage, therefore, between the first and second element vector<char>there will be no "filling" if it bothers you. A specific requirement for std::vectora fact that 0 < n < vec.size(), &vec[n] == &vec[0] + n.

[: , : , , , value_type. , - , . , .]

++ 1-, , , , vector<bool>. , std::vector<char> . , , , 4-; -)

, - , ++ . , , . , . , , , , , .

+9

, , std::vector 4 ( int ).

, std::vector C. , : - /etc, std::vector<N> v C &v[0]. ( , .)

- , ?

, , ( ). /etc ( ) , , , , .

(, std::list std::map) , , new. new ( , malloc()), , (*). , , , . std::vector, , STL- : new , new[].

(*) ++ " (basic.stc.dynamic.allocation), (expr.new) , ". malloc(), , POSIX: " , , , [...], ++ : char char, .

+2

? , /. Windows , #pragma pack().

, , , , , .

+1

. (void *), 4 8 , .

0

() , / :

// allocation
char* pointer = _mm_malloc(size, alignment);
// deallocation
_mm_free(pointer);
0
source

All Articles