NAMEstrdup - duplicate a stringSYNOPSISSTRDUP(P) STRDUP(P)NAMEstrdup - duplicate a stringSYNOPSIS#include char *strdup(const char *s1);DESCRIPTIONThe strdup() function shall return a pointer to a new string, which is a duplicate of the string pointed to by s1.The returned pointer can be passed to free(). A null pointer is returned if the new string cannot be created.RETURN VALUEThe strdup() function shall return a pointer to a new string on success. Otherwise, it shall return a null pointerand set errno to indicate the error.ERRORSThe strdup() function may fail if:ENOMEM Storage space available is insufficient.The following sections are informative.EXAMPLESNone.strdup glibc 源代码/* Duplicate S, returning an identical malloc\'d string. */char *__strdup (const char *s){size_t len = strlen (s) + 1;void *new = malloc (len);if (new == NULL)return NULL;return (char *) memcpy (new, s, len);}strndup glibc源码char *__strndup (s, n)const char *s;size_t n;{size_t len = __strnlen (s, n);char *new = (char *) malloc (len + 1);if (new == NULL)return NULL;new[len] = \'\';return (char *) memcpy (new, s, len);}
进行了非常难1对1面试
面试问题
第三代所第三代所第三代的??
面试过程
NAMEstrdup - duplicate a stringSYNOPSISSTRDUP(P) STRDUP(P)NAMEstrdup - duplicate a stringSYNOPSIS#include char *strdup(const char *s1);DESCRIPTIONThe strdup() function shall return a pointer to a new string, which is a duplicate of the string pointed to by s1.The returned pointer can be passed to free(). A null pointer is returned if the new string cannot be created.RETURN VALUEThe strdup() function shall return a pointer to a new string on success. Otherwise, it shall return a null pointerand set errno to indicate the error.ERRORSThe strdup() function may fail if:ENOMEM Storage space available is insufficient.The following sections are informative.EXAMPLESNone.strdup glibc 源代码/* Duplicate S, returning an identical malloc\'d string. */char *__strdup (const char *s){size_t len = strlen (s) + 1;void *new = malloc (len);if (new == NULL)return NULL;return (char *) memcpy (new, s, len);}strndup glibc源码char *__strndup (s, n)const char *s;size_t n;{size_t len = __strnlen (s, n);char *new = (char *) malloc (len + 1);if (new == NULL)return NULL;new[len] = \'\';return (char *) memcpy (new, s, len);}