6 ms·
case OPTTYPE_KEYMAP: str = malloc(1 + sizeof "submenu"); strcpy(str, "submenu"); return str; There isn't a good reason for this not to
by swolchok 10y ago
case OPTTYPE_KEYMAP:
str = malloc(1 + sizeof "submenu");
strcpy(str, "submenu");
return str;
There isn't a good reason for this not to be
return strdup("submenu");
is there?
- vinkelhake 10y agomalloc+strcpy is standard C, strdup is not.
- deathanatos 10y agoWhile I agree, strdup is POSIX. In the exceedingly rare event that you're on a system without it, it's simple enough to roll your own. (And if this occurred more than a couple of times, I'd roll my own anyways to keep the code obvious.)
- theseoafs 10y agoIn the exceedingly rare event that you need to target Windows?
- michael_h 10y ago#define strdup _strdup
- pjmlp 10y agoWindows isn't the only non-POSIX OS out there, maybe for the HN crowd I guess.
- deathanatos 10y agoHonestly, when I wrote the post, I thought I recalled that Windows had strdup (Windows does include some limited POSIX functionality, IIRC; e.g., "select" definitely exists, and is also a POSIX function, so I figured surely strdup was there.) MSDN seems to indicate that Windows does in fact have strdup — and that it's "deprecated" in favor of "_strdup", which appears to do exactly the same thing as strdup… what on earth is going on here?
- swolchok 10y agoNethack does have a lot of ports to weird systems (and compilers that might not special-case strdup on a string literal to avoid the strlen, which I didn't think of when I brought this up in the first place), so this seems like a fair enough reason to me.
- hamiltonkibbe 10y agoI'd wager a significant amount of production C is running on systems without ANY OS
- cremno 10y ago>1 + That's unnecessary. But then again I can't really blame them. It isn't always clear when you have to do that. Also a previous version might have used strlen() instead of sizeof.