Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...