NAME<br><br> strdup - duplicate a string<br><br>SYNOPSIS<br><br>STRDUP(P) STRDUP(P)<br><br>NAME<br><br> strdup - duplicate a string<br><br>SYNOPSIS<br><br> #include <string.h><br><br> char *strdup(const char *s1);<br><br>DESCRIPTION<br><br> The strdup() function shall return a pointer to a new string, which is a duplicate of the string pointed to by s1.<br><br> The returned pointer can be passed to free(). A null pointer is returned if the new string cannot be created.<br><br>RETURN VALUE<br><br> The strdup() function shall return a pointer to a new string on success. Otherwise, it shall return a null pointer<br><br> and set errno to indicate the error.<br><br>ERRORS<br><br> The strdup() function may fail if:<br><br> ENOMEM Storage space available is insufficient.<br><br> The following sections are informative.<br><br>EXAMPLES<br><br> None.<br><br>strdup glibc 源代码<br><br>/* Duplicate S, returning an identical malloc'd string. */<br><br>char *<br><br>__strdup (const char *s)<br><br>{<br><br>size_t len = strlen (s) + 1;<br><br>void *new = malloc (len);<br><br>if (new == NULL)<br><br> return NULL;<br><br>return (char *) memcpy (new, s, len);<br><br>}<br><br>strndup glibc源码<br><br>char *<br><br>__strndup (s, n)<br><br> const char *s;<br><br> size_t n;<br><br>{<br><br>size_t len = __strnlen (s, n);<br><br>char *new = (char *) malloc (len + 1);<br><br>if (new == NULL)<br><br> return NULL;<br><br>new[len] = '';<br><br>return (char *) memcpy (new, s, len);<br><br>}
微软中国(Microsoft)软件工程师面试经验&面试问题
面试问题
第三代所第三代所第三代的?
面试过程
NAME<br><br> strdup - duplicate a string<br><br>SYNOPSIS<br><br>STRDUP(P) STRDUP(P)<br><br>NAME<br><br> strdup - duplicate a string<br><br>SYNOPSIS<br><br> #include <string.h><br><br> char *strdup(const char *s1);<br><br>DESCRIPTION<br><br> The strdup() function shall return a pointer to a new string, which is a duplicate of the string pointed to by s1.<br><br> The returned pointer can be passed to free(). A null pointer is returned if the new string cannot be created.<br><br>RETURN VALUE<br><br> The strdup() function shall return a pointer to a new string on success. Otherwise, it shall return a null pointer<br><br> and set errno to indicate the error.<br><br>ERRORS<br><br> The strdup() function may fail if:<br><br> ENOMEM Storage space available is insufficient.<br><br> The following sections are informative.<br><br>EXAMPLES<br><br> None.<br><br>strdup glibc 源代码<br><br>/* Duplicate S, returning an identical malloc'd string. */<br><br>char *<br><br>__strdup (const char *s)<br><br>{<br><br>size_t len = strlen (s) + 1;<br><br>void *new = malloc (len);<br><br>if (new == NULL)<br><br> return NULL;<br><br>return (char *) memcpy (new, s, len);<br><br>}<br><br>strndup glibc源码<br><br>char *<br><br>__strndup (s, n)<br><br> const char *s;<br><br> size_t n;<br><br>{<br><br>size_t len = __strnlen (s, n);<br><br>char *new = (char *) malloc (len + 1);<br><br>if (new == NULL)<br><br> return NULL;<br><br>new[len] = '';<br><br>return (char *) memcpy (new, s, len);<br><br>}