3 ms·
Note to future me: I checked the C++98 standard. a[i] is defined as *(a+i), the size of the array is not mentioned. So if a points somewhere where a+i is a lega
by adrianN 11y ago
Note to future me: I checked the C++98 standard. a[i] is defined as *(a+i), the size of the array is not mentioned. So if a points somewhere where a+i is a legal access, this should be fine.
Edit: But casting the allocated memory around to a struct type might violate aliasing rules...
- albinofrenchy 11y agoAs far as I know, even things like: uint16_t array[] = { 1, 2, 3, 4 }; cout << "Hello World " << 3[array] << endl; are valid and well defined. Part of what makes memory access validation such a pain in the ass in C++ is that no attempts are really made at specifying rules for how you can come up with memory locations; just rules on which locations you can read and write from. You can do things like "((int)rand()) = 2", and nothing in the standard says this is an invalid thing to do, so long as you are lucky enough for rand() to point somewhere allocated.