4 ms·
When hiring "Experienced C Programmers", I would just show them a the short version of strcpy() out of K&R, but with variable names change from 'src' and 'dst'
by LocalMan 13y ago
When hiring "Experienced C Programmers", I would just show them a the short version of strcpy() out of K&R, but with variable names change from 'src' and 'dst' to 'x' and 'y'.
Just to read it and say what it did. This eliminated the vast majority of the applicants:
void strcpy(char x, char y)
{
while(x++ = y++);
}
- dnautics 13y agoI had no idea that's how strcpy is implemented. Buffer overflow exploits and the like aside (ah the good old days) that is really elegant!
- huhtenberg 13y agoThis is not how strcpy is implemented. It is typically done in assembly.
- deleted 13y ago[deleted]
- andrewcooke 13y agohttps://sourceware.org/git/?p=glibc.git;a=blob;f=string/strcpy.c;h=b71f7536bca7fd2afc5854f681a3629cee2cab6f;hb=HEAD https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strc...
- e12e 13y agoReally? I just did a "apt-get source libc6" and in strings/eglibc-2.17/string/strcpy.c I found: /* Copy SRC to DEST. */ char * strcpy (dest, src) char *dest; const char *src; { char c; char *__unbounded s = (char *__unbounded) CHECK_BOUNDS_LOW (src); const ptrdiff_t off = CHECK_BOUNDS_LOW (dest) - s - 1; size_t n; do { c = *s++; s[off] = c; } while (c != '\0'); n = s - src; (void) CHECK_BOUNDS_HIGH (src + n); (void) CHECK_BOUNDS_HIGH (dest + n); return dest; }
- peff 13y agoThat is the fallback implementation in case a platform does not have an optimized version available. Try `sysdeps/x86_64/strcpy.S` to see an optimized version.
- e12e 13y agoOooh, shiny. I'm not very familiar with how this is all tied together, thanks for the pointer. I was a bit thrown by the apparent lack of ifdefs and such in the c definition, but I see the asm-version starts out with: #ifndef USE_AS_STPCPY # define STRCPY strcpy #endif So I guess there is some logic set up in the configure-stage before building.
- troels 13y agoI assume the caller has the responsibility to ensure that x is actually large enough to contain y.
- tel 13y agoThey have to ensure that the strings are \0 terminated, so why not? C is definitely pre-condition-or-else.
- noir_lord 13y agoYou are probably right (and this is one of the reasons why as a language C and C++ can really blow up in your face).
- dnautics 13y agothat's why there are alternatives such as strncat.
- Nate75Sanders 13y agoMarkdown ate the asterisks in the code snippet. Important to realize that if you're not terribly familiar with C.
- jk4930 13y agoI'm not an experienced C programmer but I assume there's missing some '*' somewhere?
- sp332 13y agoTry putting two spaces at the beginning of the line. void strcpy(char *x, char *y) { while(*x++ = *y++); }
- w0rd-driven 13y agoFor most markdown flavors, a forward slash before the character (\) should do the trick. [Re-Edit] Finally saw the help and HN isn't typical markdown. 2 spaces for pre tags only.
- sp332 13y agoIt's not markdown. https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc There are really only two things you can format on HN: italics (with asterisks) and literal lines (with two leading spaces).
- OseOse 13y agovoid strcpy(char *x, char *y) { while(*x++ = *y++); } In this example, why does the loop terminate? Is it because the null terminator counted as a false?
- deleted 13y ago[deleted]
- tel 13y agoExactly, since C strings are just pointers to raw memory you usually assume that either the user passes the length explicitly to you or that they're \0-terminated.