Skip to content

Instantly share code, notes, and snippets.

@nikhilmetrani
Last active August 12, 2019 16:45
Show Gist options
  • Save nikhilmetrani/ebc478f1466a2105430deb9f8cd40371 to your computer and use it in GitHub Desktop.
Save nikhilmetrani/ebc478f1466a2105430deb9f8cd40371 to your computer and use it in GitHub Desktop.
C++ Memory alignment
#include <cstdint>
uint32_t fun(char *c1) {
// if c1 is pointing to memory address 1004, it is aligned access
// if its pointing to 1005, it us unalighed access
// return *c1; // some systems do this
// return c1[0] | c1[1] << 8; // some do this (worst than before)
return *((uint32_t*) c1); // This is the performance improvement, does not work on web assembly
}
@nikhilmetrani
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment