strdup or _strdup?

C++CVisual StudioStrdup

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

> Which is correct?

strdup is a perfectly correct POSIX function. Nevertheless, it doesn't belong to the standard, and the ANSI C standard reserves some (broad) classes of function names for further use. Among these, there are

  • Function names that begin with str and a lowercase letter

therefore, the MS guys decided to replace strdup with _strdup.

I'd just continue using strdup. It's unlikely the C committee will define strdup to something else than POSIX. Either #define strdup _strdup or silence the warning.

BTW I hope you see this applies to your functions with names like string_list etc., too.

Solution 2 - C++

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.

Solution 3 - C++

Solution 4 - C++

If you just want to avoid the Warning message:

> Project-> property -> C/C++ -> Preprocessor -> Preprocessor Definitions

Edit this, and add

> _CRT_NONSTDC_NO_DEPRECATE

Solution 5 - C++

strdup is POSIX:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/strdup.html

_strdup is Windows specific:

http://msdn.microsoft.com/en-us/library/y471khhc(v=vs.80).aspx

On Unix, use strdup. On Windows, use _strdup. It's that simple. If you need to write portable code between Unix and Windows:

  • use system dependent macros (for example _WIN32 vs. _POSIX_VERSION) to select the proper function (but notice that the macros may depend on specific pre existing include files):

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html

http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx

  • use standard functions to reimplement strdup: strlen, malloc and memmove.

  • use a cross platform utility library, like glib:

http://developer.gnome.org/glib/2.28/glib-String-Utility-Functions.html#g-strdup

Note that Visual C++ message suggests that _strdup belongs to the C++ standard, but this is false, as it can be verified on the C++ standard. It merely uses the underscore prefix as a "namespace" for the function.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3376.pdf

Solution 6 - C++

Don't know about C++.

The C Standard does not describe any function with the strdup name (though the name is reserved). To be portable, in C, you're better off replacing that with malloc, strcpy, and free.

Solution 7 - C++

it's not a warning but an error reported in higher version of vs.

use macro #ifdef WIN32 to switch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmir SaniyanView Question on Stackoverflow
Solution 1 - C++jpalecekView Answer on Stackoverflow
Solution 2 - C++Cheers and hth. - AlfView Answer on Stackoverflow
Solution 3 - C++David BremnerView Answer on Stackoverflow
Solution 4 - C++DarósView Answer on Stackoverflow
Solution 5 - C++hdanteView Answer on Stackoverflow
Solution 6 - C++pmgView Answer on Stackoverflow
Solution 7 - C++yjjjnlsView Answer on Stackoverflow