1

When defining a stream of data for a program to read into memory (a file format for e.g. but could be any other kind of data streamed into memory).

How should the data be alignment for optimal memcpy?

While memcpy doesn't require alignment, I've noticed its common for formats to write pad-bytes, aligning to 4 bytes for example (even when the file's aren't memory dumps).

I assume this is because its faster to operate on aligned data, however I'm not really sure about the details.


For common architectures, (X86, x64, ARM, PPC), (32 / 64 bits), on common libc implementations (glibc, freebsd, microsoft's... etc) ... is there some rule of thumb for aligning data for optimal reading into memory?

  • Should 32 bit systems align to 4 and 64bit align to 8?
  • Or is this unnecessary, where a fixed alignment is sufficient (4 seems common) for popular architectures - irrespective of their bitness?
  • Or should (ideally) the data be aligned to each structures _Alignof?
    ... which may be impractical to implement since this will vary between systems.
    (making it preferable to fallback to some fixed value to simplify reading/writing)
ideasman42
  • 42,413
  • 44
  • 197
  • 320

0 Answers0