c++11 - C++ alignment - strict and weak -
i'm reading n3485 c++ standard draft , in section on alignment, states following:
3.11 alignment [basic.align]
1 ... alignment implementation-defined integer value representing number of bytes between successive addresses @ given object can allocated. object type imposes alignment requirement on every object of type; stricter alignment can requested using alignment specifier (7.6.2).
here states alignment is, , that. means if have 32 bit (4byte) system, read in 4 byte size chunks @ time, should place things in memory @ 4 byte offset addresses. lot of handled compiler, c++ lets have in too.
the alignment-specifier speaks of alignas
.
you can pass integer alignas
specify alignment requirement or type alignas(double)
now, have weak or strict alignment, stated following:
5 alignments have order weaker stronger or stricter alignments. stricter alignments have larger alignment values. address satisfies alignment requirement satisfies weaker valid alignment requirement.
7 comparing alignments meaningful , provides obvious results:
— 2 alignments equal when numeric values equal.
— 2 alignments different when numeric values not equal.
— when alignment larger represents stricter alignment.
so here states stricter alignment larger alignment. mean alignas(8)
more strict alignas(4)
? on 32 bit (4 byte) system, 8 version mean int
type require 2 reads instead of one, , 1 padding. strict referring fact imposes potential overhead on cpu, when alignment "misused", in silly example?
i realize not alignments valid on given system, , extended alignments have valid in order produce correct behavior.
to me alignment letting cpu access memory data in word size chunks, optimal way cpu fetch , write data , memory.
this strict alignment, understand it, potentially ruin that. wrong? strict alignment vs weak alignment?
the "strict" merely refers fact larger alignment values more restrictive in terms of possible addresses.
there half many valid addresses alignment 8 there alignment 4, type alignment 8 more restricted regarding can placed.
Comments
Post a Comment