Amir Saniyan Amir Saniyan - 1 month ago 25
C Question

strdup or _strdup?

When I use

strdup
in Microsoft Visual C++, it warns me:


warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details.


Thus it seems
_strdup
is correct.

But when I use
_strdup
in GCC (Fedora Linux OS), the compiler shows an error:


error: ‘_strdup’ was not declared in this scope


With GCC and Linux, compiler does not show any error for
strdup
.

Which is correct -
strdup
or
_strdup
?

Note: I include
<string.h>
in my code.

Answer

strdup is not a standard C++ function. but it is apparently a Posix function, and anyway it's a well known function which has been there since K&R C. so if you absolutely must use it, do not fret about any possible name collision, and just write strdup for maximum portability.

Comments