C++ strdup 或 _strdup?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7582394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
strdup or _strdup?
提问by Amir Saniyan
When I use strdup
in Microsoft Visual C++, it warns me:
当我strdup
在 Microsoft Visual C++ 中使用时,它警告我:
warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details.
警告 C4996:“strdup”:不推荐使用此项的 POSIX 名称。而是使用符合 ISO C++ 的名称:_strdup。详细信息请参见在线帮助。
Thus it seems _strdup
is correct.
因此它似乎_strdup
是正确的。
But when I use _strdup
in GCC (Fedora Linux OS), the compiler shows an error:
但是当我_strdup
在 GCC (Fedora Linux OS) 中使用时,编译器显示错误:
error: ‘_strdup' was not declared in this scope
错误:“_strdup”未在此范围内声明
With GCC and Linux, compiler does not show any error for strdup
.
使用 GCC 和 Linux,编译器不会显示任何错误strdup
。
Which is correct - strdup
or _strdup
?
哪个是正确的 -strdup
还是_strdup
?
Note: I include <string.h>
in my code.
注意:我包含<string.h>
在我的代码中。
采纳答案by Cheers and hth. - Alf
strdup
is not a standard C++ function. but it is apparently a Posix function, and anyway it's a well known functionwhich 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.
strdup
不是标准的 C++ 函数。但它显然是一个 Posix 函数,无论如何它是一个众所周知的函数,自 K&R C 以来一直存在。因此,如果您绝对必须使用它,请不要担心任何可能的名称冲突,只需编写strdup
以获得最大的可移植性。
回答by jpalecek
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
strdup
是一个完全正确的 POSIX 函数。然而,它不属于标准,ANSI C 标准保留了一些(广泛的)函数名称类以供进一步使用。其中,有
- Function names that begin with strand a lowercase letter
- 以str和小写字母开头的函数名
therefore, the MS guys decided to replace strdup
with _strdup
.
因此,MS 家伙决定替换strdup
为_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.
我只是继续使用strdup
. C 委员会不太可能定义strdup
POSIX 以外的其他东西。要么#define strdup _strdup
关闭警告。
BTW I hope you see this applies to your functions with names like string_list
etc., too.
顺便说一句,我希望你看到这也适用于你的函数,比如string_list
等。
回答by David Bremner
You can #define _CRT_NONSTDC_NO_DEPRECATEto disable this warning.
您可以 #define _CRT_NONSTDC_NO_DEPRECATE禁用此警告。
回答by Darós
If you just want to avoid the Warning message:
如果您只想避免出现警告消息:
Project-> property -> C/C++ -> Preprocessor -> Preprocessor Definitions
项目 -> 属性 -> C/C++ -> 预处理器 -> 预处理器定义
Edit this, and add
编辑这个,并添加
_CRT_NONSTDC_NO_DEPRECATE
_CRT_NONSTDC_NO_DEPRECATE
回答by hdante
strdup is POSIX:
strdup 是 POSIX:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/strdup.html
http://pubs.opengroup.org/onlinepubs/9699919799/functions/strdup.html
_strdup is Windows specific:
_strdup 是 Windows 特定的:
http://msdn.microsoft.com/en-us/library/y471khhc(v=vs.80).aspx
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:
在 Unix 上,使用 strdup。在 Windows 上,使用 _strdup。就这么简单。如果您需要在 Unix 和 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):
- 使用系统相关宏(例如 _WIN32 与 _POSIX_VERSION)来选择正确的函数(但请注意,宏可能取决于特定的预先存在的包含文件):
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx
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:
使用标准函数重新实现 strdup:strlen、malloc 和 memmove。
使用跨平台实用程序库,如 glib:
http://developer.gnome.org/glib/2.28/glib-String-Utility-Functions.html#g-strdup
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.
请注意,Visual C++ 消息表明 _strdup 属于 C++ 标准,但这是错误的,因为它可以在 C++ 标准上进行验证。它仅使用下划线前缀作为函数的“命名空间”。
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3376.pdf
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3376.pdf
回答by pmg
Don't know about C++.
不知道 C++。
The C Standarddoes 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
.
在C标准并没有描述与任何函数strdup
名(虽然名字被保留)。为了便于移植,在C,你最好更换与malloc
,strcpy
和free
。
回答by yjjjnls
it's not a warning but an error reported in higher version of vs.
这不是警告,而是在更高版本的 vs. 中报告的错误。
use macro #ifdef WIN32
to switch
使用宏#ifdef WIN32
切换