80989

Why do we have alignment padding if memory is byte-addressable?

Question:

Since we can address every byte of memory individually, why do compilers take extra care to make sure that structs and it's members align to 32-bit borders in memory? I could be wrong here, but on a 32-bit system, is it not just as fast to get 4 bytes starting from say 0x0800, as it is from 0x0801?

Answer1:

On most architectures it is faster to perform read/write on naturally aligned data types. On <em>some</em> systems it will generate an exception (i.e. <em>crash</em> in most cases) if you try to access certain types when they are misaligned. So in general you always want to maintain natural alignment unless you have a very good reason not to.

See also related SO questions and answers:

<ul><li>

<a href="https://stackoverflow.com/questions/381244/purpose-of-memory-alignment" rel="nofollow">Purpose of memory alignment</a>

</li> <li>

<a href="https://stackoverflow.com/questions/2006216/why-is-data-structure-alignment-important-for-performance" rel="nofollow">why is data structure alignment important for performance?</a>

</li> <li>

<a href="https://stackoverflow.com/questions/1496848/does-unaligned-memory-access-always-cause-bus-errors" rel="nofollow">Does unaligned memory access always cause bus errors?</a>

</li> </ul>

Answer2:

Taken from <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" rel="nofollow">wikipedia</a>:

<blockquote>

For example, when the computer's word size is 4 bytes (a byte meaning 8 bits), the data to be read should be at a memory offset which is some multiple of 4. When this is not the case, e.g. the data starts at the 14th byte instead of the 16th byte, then the computer has to read two 4-byte chunks and do some calculation before the requested data has been read, or it may generate an alignment fault. Even though the previous data structure ends at the 14th byte, the next data structure should start at the 16th byte. Two padding bytes are inserted between the two data structures to align the next data structure to the 16th byte

</blockquote>

The memory has to be multiple of 4 bytes for faster access and to reduce computation for better performance. so if the memory is address byte addressable usually of 4 bytes chunks in most of cases then we know where the next address is going to start e.g. as explained above also if you end up with 14 bytes (that should be 16 bytes 4*4 = 16) then you know how much padding you have to use 16-14 = 2 bytes padding. that is why padding is used in misaligned memory.

Recommend

  • Compression in node.js
  • Semantic of startsWith on Arrays
  • jQuery data value not updating
  • SQL complicated recursive CTE
  • Could be Text orientation of JTextArea changed by keyboard shortcut?
  • Zooming an element and its contents— an alternative to CSS3's zoom property?
  • How can I count unique terms in a plaintext file case-insensitively?
  • Does the Azure table storage API cache results?
  • Put value at centre of bins for histogram
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • Cast between interfaces whose interface signatures are same
  • Overlapping controls in Windows XP
  • SetWindowsHookEx does not react on media keys
  • Unable to get column index with table.getColumn method using custom table Model
  • How can I speed up CURL tasks?
  • Ensure fsync did its job
  • Transactional Create with Validation in ServiceStack Redis Client
  • Hardware Accelerated Image Scaling in windows using C++
  • C++ Partial template specialization - design simplification
  • Email format validation in mvc3 view
  • Javascript simulate pressing enter in input box
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • How to add date and time under each post in guestbook in google app engine
  • TFS: Get latest causes slow project reloading
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • ActionScript 2 vs ActionScript 3 performance
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Suggestions to manage Login/Logout transitions
  • Why joiner is not used after Sequence generator or Update statergy
  • coudnt use logback because of log4j
  • embed rChart in Markdown
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Android Heatmap on canvas or ImageView