strdup or _strdup?

后端 未结 7 2114
我寻月下人不归
我寻月下人不归 2020-12-01 04:05

When I use strdup in Microsoft Visual C++, it warns me:

warning C4996: \'strdup\': The POSIX name for this item is deprecated. Instead, u

7条回答
  •  情书的邮戳
    2020-12-01 04:34

    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

提交回复
热议问题