On cpp reference page for strdup, I notice it says,
As all functions from Dynamic Memory TR, strdup is only guaranteed to be available if
__STDC_ALLOC_LIB__
is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT2__
to the integer constant 1 before including string.h.
I thought that what the function strdup
do is just simply malloc
a space and strcpy
the source to that space, and I also found its implementation in glibc.
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);
}
There's nothing special in its implementation, so why shall I define __STDC_WANT_LIB_EXT2__
to integer 1 before using strdup
(or any other Dynamic Memory TR)? What does defining __STDC_WANT_LIB_EXT2__
do here?