4 ms·
it's illegal to dereference a pointer past the end of an allocation. Yes, but there's a legal way to do this. reference the other two areas with offsets relat
by cperciva 13y ago
it's illegal to dereference a pointer past the end of an allocation.
Yes, but there's a legal way to do this.
reference the other two areas with offsets relative to the first area
If you cast the pointers to uintptr_t, and perform your arithmetic on uintptr_t and cast your final pointer back (void * ) before using it, what you've done is perfectly legal and safe (albeit weird) since uintptr_t is an unsigned integer type with all the flexibility of unsigned integers, and if x = (uintptr_t)p then it's guaranteed that p == (void * )x.
In other words:
char * x = malloc(100);
char * y = malloc(100);
ptrdiff_t yminusx = y - x;
x[yminusx] = '\0'
is undefined behaviour, but
char * x = malloc(100);
char * y = malloc(100);
uintptr_t yminusx = (uintptr_t)(y) - (uintptr_t)(x);
*(char *)(void *)((uintptr_t)(x) + yminusx) = '\0'
is valid C (and has the same meaning on a DWIM compiler).
- deleted 13y ago[deleted]
- rayiner 13y agoRight. You could also convert a pointer to a uintptr_t, XOR it with some value, then at some later point XOR it again to get back the original address and cast it back to a pointer and dereference it, and it would be legal despite breaking GC. However, a number of things that also break GC, like your first example, also happen not to be legal C even though they'll probably work on any existing implementation.
- Someone 13y agoThat's very close to the classical example of code that maims pointers: the doubly linked list that uses only one 'pointer' in each node (http://www.geeksforgeeks.org/xor-linked-list-a-memory-efficient-doubly-linked-list-set-1/ http://www.geeksforgeeks.org/xor-linked-list-a-memory-effici...)
- bcoates 13y agoIt looks like the second is still valid C, but a C++11 compiler is allowed to break it if it declares "strict pointer safety" rules are in effect at runtime (which a GC implementation would presumably do)
- logancapaldo 13y agoWhat if it's not a DWIM compiler? I'm pretty sure (happy to be corrected) pointer <-> integer conversions are implementation defined. Therefore casting between could perform some reversible operation that ensured round tripping worked, but does not require that ptr(a - b) == a - b even though a == int(ptr(a)) holds.
- cperciva 13y agoI'm pretty sure (happy to be corrected) pointer <-> integer conversions are implementation defined. Quite correct, subject to the requirement that converting the same uintptr_t value back to (void ) will return the original pointer. So the conversion could be implemented as a hash table if the compiler author wanted. But if you look carefully at what I wrote, I'm never converting a value which was not previously returned by a (void )->(uintptr_t) conversion.
- logancapaldo 13y agoI see. What I was worried about was a version of the code you didn't actually write (casting the ptrdiff_t result to a [u]intptr_t, not casting the pointers first).