Let's have a char
array:
char arr[(size_t)PTRDIFF_MAX + (size_t)2];
And let's accept that we have a system with sufficient memory available.
Accessing the array either in array notation or pointer notation is defined, undefined, or implementation defined behaviour?
char c = arr[(size_t)PTRDIFF_MAX + (size_t)1];
char d = *(&arr[0] + ((size_t)PTRDIFF_MAX + (size_t)1));
I'm concerned that the index may be converted to ptrdiff_t
before the access resulting in an invalid index.
Edit:
In the case there's no possibility of having an array of that many elements, let's put this similar situation:
We have an array of less elements than PTRDIFF_MAX
, but big ones (e.g.: int64_t
, or structs with many elements), such that the raw size of the array still exceeds PTRDIFF_MAX
. And we access the array through a char *
, which is a valid cast (more or less what's happening inside memcpy
).