Explain how a buffer overflow vulnerability occurs in C++ and how modern compilers and operating systems mitigate this risk.