This C++ program will crash because of a type punned pointer dereference. The reason this happens is that the compiler doesn’t know what to do with two pointers at the same memory location, so it throws an error and stops executing the code. This behavior isn’t limited to the C++ programming language, pointers are not type punned in any languages. The reason for this is that when you dereference a pointer, it doesn’t matter what kind of data it’s pointing at. It just points to more memory and let’s you access whatever is there; which will also include other types of pointers! This may sound like something good but if two non-compatible pointers get mixed up then bad things happen because both sides don’t know how to handle each other either. Type punning can be used as an optimization technique so we’ll explore this later on in the article series with some examples about why using it might have benefits for your code. The first example below

LEAVE A REPLY

Please enter your comment!
Please enter your name here