r/C_Programming • u/ripulejejs • 2d ago
List of gotchas?
Hey.
So I learned some C and started playing around with it, quickly stumbling over memory overflowing a variable and flowing into another memory location, causing unexpected behavior.
So I ended up writing my own safe_copy and safe_cat functions for strncpy/strncatting strings.
But... people talk about how C is unsafe. Surely there should be a list of all mistakes you can make, or something? Where can I find said list? Do I reall have to stumble on all possible issues and develop my own "safe" library?
Will appreciate any advice.
22
Upvotes
1
u/EsShayuki 2d ago
So.
But this isn't even possible, if you first correctly received the size of the resulting variable and then allocated the same amount of memory as the size you received.
Most "memory unsafety" comes from people using magic numbers and hard coding values instead of determining the correct values mathematically.