Last active
December 30, 2015 18:49
-
-
Save chadaustin/7870504 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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). | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is well formed because there are no intervening possibly aliasing accesses to i.