logoalt Hacker News

throwaway2037 • today at 10:54 AM • 1 reply • view on HN

    > That's why it's recommended to use size_t for passing sizes of buffers.
I have always been told it is a bad idea to use unsigned integers in your interfaces. Why? An accidental negative (signed) value will be cast into an enormous unsigned value. For example, if you look at the vast majority of the Win32 API, inputs are normally signed integers. They rarely use unsigned integers.

Replies

Panzerschrek • today at 4:02 PM

> An accidental negative (signed) value will

If you use mostly unsigned values it's not a huge problem, as long as you don't perform subtractions or operations with possibility of overflow. If obtaining a negative value or getting an overflow may occur, you should perform proper checks before calling an API function. And in case of WinAPI or something similar usually no extra computations before a call are necessary, arguments are already precomputed (like with buffer sizes).