In C, itโs great to make a pointer that points to the last one element of the array and use it in pointer arithmetic if you don't find it:
int a[5], *p = a+5, diff = pa;
However, this is UB:
p = a+6; int b = *(a+5), diff = pa;
Now I have a question: does this apply to dynamically allocated memory? Suppose I use a pointer pointing to one of the last in pointer arithmetic, without dereferencing it, and malloc() succeeds.
int *a = malloc(5 * sizeof(*a)); assert(a != NULL, "Memory allocation failed");
c language-lawyer malloc dynamic-allocation bounds
iBug Dec 20 '17 at 7:03 2017-12-20 07:03
source share