This works by accident. Your malloc will most likely highlight a chunk significantly larger than one character (usually because tracking nanoscopic details is not worth the overhead). That way, you are probably tromping outside the space you should have, but that doesn't matter in your implementation.
Your program is still erroneous, I think, in accordance with the standard. And if it was a lot more, and you made this mistake in places where it did not work by chance, you will get all kinds of bad behavior, sometimes illegal access to memory, but sometimes just completely crazy behavior, because you can damage your own or system data structures.
Our CheckPointer tool should recognize this error; technically, you save outside the requested area and discover it with sufficient effort.
source share