r/C_Programming • • Sep 12 '26

Question What C function would you delete and rewrite first, and why?

For me it's fnmatch - mostly because it's 40 year's old and smell's funny.

97 Upvotes

238 comments sorted by

View all comments

Show parent comment

2

u/BlockOfDiamond Sep 13 '26

For struct arrays, the caller could just multiply the size by the count manually. The only advantage I could think of is avoiding overflows on systems with a narrow size_t.

1

u/LB-- Sep 14 '26

Not just that. Where multiplication might overflow, the read or write could still succeed, so the implementation could "do the right thing" without making you manually write careful code for it. Pretty obscure scenario nowadays though.

2

u/BlockOfDiamond Sep 14 '26

Yeah, that is what I meant.

1

u/LB-- Sep 14 '26

Ahh fair, I thought you were only talking about overflow checking, which is separate from what I was talking about.

2

u/BlockOfDiamond Sep 14 '26

If the product of the element size and count would overflow, then passing them separately to fread would still behave correctly even though multiplying them would not.

1

u/flatfinger Sep 14 '26

Some file systems support a "read up to N records of up to B bytes" function, whose behavior may be different from a request to read 1 record of up to N*B bytes, or N*B one-byte records. While the Standard would not require that implementations allow C programs to read any files that C programs didn't create, such an ability is often useful nonetheless, even if it exposes some environment-specific quirks.