Hacker News new | ask | show | jobs
by Tharre 2457 days ago
Quite frankly, this site's examples are pretty horrible.

Case in point, Idiom #46 in C[0] uses strncpy and predictably fails to correctly terminate the buffer.

Idiom #55[1] converts a integer to a string with itoa, never bothering to mention that it isn't part of the C standard or POSIX, while for some reason using a 4096 byte big output buffer.

Idiom #39[2] first example uses clrscr(), which I assume is some sort of old DOS function? The entire example looks unrelated to the problem.

The idea is certainly good, but you need some serious vetting system to make this usable.

[0] https://www.programming-idioms.org/idiom/46/impl/477

[1] https://www.programming-idioms.org/idiom/55/impl/438

[2] https://www.programming-idioms.org/idiom/39/impl/2042

3 comments

%d for strlen is cute as well.

That’s the fate of all such sites. They start with a nice idea and a good intent, but the crowd fills it with crap, nobody cares/is able to screen it, and it instantly becomes an unreliable source of nonsense.

Ed: btw, #1 is terminated by calloc(6), but it is indeed a trip mine looking for prey. It is unknown if strncpy was used intentionally or out of ignorance and if the next person will be aware.

> #1 is terminated by calloc(6)

Yet this seems to be the perfect place and time to remind the kind audience that sizeof measures the number of char-sized chunks, thus sizeof(char) is always the constant 1, no need to spell it out.

> Case in point, Idiom #46 in C[0] uses strncpy and predictably fails to correctly terminate the buffer.

calloc returns zeroed memory.

Plus #46 casts calloc, and uses the unnecessary sizeof (char).