> It actually worked, once I spend 20 hours in valgrind
Thia is exactly the main problem with C.
You need to rely on tolling outside the language to be able to write safer code.
While languages like Ada and Modula-2 and their descendents, offer the same hardware capabilities as C with stronger type checking.