I come across this question during a discussion with an interviewer:
If I allocated 4 bytes of memory from malloc.
int *p = (int*) malloc(4);
Now, if I moved the pointer to 4 bytes.
p++;
And now the pointer points to a memory that contains 4 bytes of memory allocated by malloc. Suppose this memory has read / write permission.
*p=4;
This means that the pointer exits the allocated memory.
Now consider the case where I allocated some memory, but whenever my pointer leaves the allocated and writes to this memory, I want to be informed, but how
I only have malloc and am free to use.
source
share