Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Last active December 30, 2015 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadaustin/7870504 to your computer and use it in GitHub Desktop.
Save chadaustin/7870504 to your computer and use it in GitHub Desktop.
/* To my understanding, the following is well-defined C:
*
* given: */
int* p;
// then:
char* c = (char*)p;
char firstByteAtP = *c;
// firstByteAtP is the first byte at the pointer p.
// Put another way, aliasing through char* is OK; otherwise, memcpy could not be written.
// Now, my question: is the following code well-defined?
char* c;
intptr_t i;
memcpy(&i, &p, sizeof(p));
memcpy(&c, &i, sizeof(c));
char firstByteAtP = *c;
/*
* That is, can you alias c to p through a memcpy?
* If so, C precludes an implementation where a pointer to a wider type has a different implementation,
* such as indexing assuming aligned access.
*
* Emscripten could realize significant code size reductions if it could represent 32-bit loads and
* stores as HEAP32[p] instead of HEAP32[p >> 2], but that would require shifting the pointers whenever
* statically casting between char* and int* (such as from the result of malloc).
*/
@jwatte
Copy link

jwatte commented Dec 10, 2013

It is well formed because there are no intervening possibly aliasing accesses to i.

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