When Code Lies: Memory Models
Learn when we cannot trust code.
Code is the source of truth
What we will learn in this lesson is very platform-specific and is primarily seen in low-level languages like C/C++, Java, etc., and in multithreaded environments, if at all. Not all programmers would have seen these issues. Even experienced ones might not have heard of memory models because they rarely cause problems. Still, when they do, the issue will not be easily reproducible, will sometimes go away when we add more logs or attach a debugger, and will happen in a layer that is totally out of the programmer's control. Therefore, it is of utmost importance that we avoid these issues altogether. But first, let's look at what a memory model is and where and how things can go wrong.
Code on a file vs. code in the execution
Till now, we have relied very heavily on our understanding of the code to go after bugs. The codebase is our ultimate source of truth. But what if we told we that, though the result or effect of the code is the same, the program in a text file is not necessarily the program in execution, and at a random time, if we check the state of the process, we might see some discrepancies. Let’s consider a very simple code snippet:
Get hands-on with 1400+ tech skills courses.