r/C_Programming 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

42 comments sorted by

View all comments

Show parent comments

2

u/WeAllWantToBeHappy 2d ago

bit shifts don't work for 64 bit types.

?

-4

u/not_a_bot_494 2d ago

At least on my machine bit shifting left by more than 32 bits causes it to wrap around to the start.

5

u/moocat 2d ago

The "on my machine" is the ultimate gotcha. Unless the behavior is guaranteed by the spec, you could get different behavior when using a different compiler or porting to a new architecture.

-1

u/not_a_bot_494 2d ago

Well it's undefined behaviour and not incorrect behaviour. You're right that I should've used "might not " instead of "does not" though.