4 ms·
Nit: one past end of an array is actually fine as an address.
by leni536 4y ago
Nit: one past end of an array is actually fine as an address.
- jussij 4y agoNot always. Consider this 'test.cpp' example: #include <stdio.h> #include <string.h> struct a_struct { char a[1]; char b[2]; a_struct() { strcpy(a, ""); strcpy(b, "b"); } } x; int main() { printf("before:\n"); printf("a : '%s'\n", x.a); printf("b : '%s'\n", x.b); char *c = &1[x.a]; *c = 'c'; printf("after:\n"); printf("a : '%s'\n", x.a); printf("b : '%s'\n", x.b); //b is now corrupt } Compile and running this code: C:\temp>g++ test.cpp C:\temp>a.exe before: a : '' b : 'b' after: a : '' b : 'c'
- leni536 4y agoYes, dereferencing past the end is not fine, but the address itself is.