How to check what a pointer writes in allocated memory?

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.

+4
source share
2

Electric Fence, , .

malloc. , . . , SIGSEGV ( ).

+2

C , nativly, .

, valgrind libefence. , , , .

( ), .

, , .., /, . , , PHP python, .

+1

All Articles