Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

strdup in C++

807575Apr 23 2004 — edited Apr 29 2004
Hello,

I made today minor modifiation to one of our C++ files. What I did was that I removed include of <iostream> because in that .cc file there were not anymore need for it. To my surprise SunONE compiler (CC: Sun C++ 5.5 2003/03/12) started to complain that "strdup" must have a prototype:

"XYZ.cc", line XYZ: Error: The function "strdup" must have a prototype.

If I then put back the #include <iostream> line no errors or warnings were seen. I then started to follow the include chain that <iostream> makes and found out that actually the <iosfwd> was the file that had to be included to avoid above error. In fact it was enaough to have these two lines from <iosfwd> to avoid the error:

#include <rw/stdmutex.h>
#include <rw/traits>

and even more exact if the #include <rw/traits> is commented out then the above error is seen. This far I had interest to find it out what happens, but then it would have gone too detailed for me to follow further down the chain. Anyhow you can play with this simple test program by commenting out or not commenting out the 4th line and compiling it with CC without options (for example).

-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
#include <cstdlib>
#include <cstring>
#include <rw/stdmutex.h>
#include <rw/traits>

int main( int argc, char** argv )
{
char* v = strdup( argv[0] );
free( v );
}
-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----

What I then found interesting is that in ANSI C++ standard it is said that <cstring> brings the str* functions of the ANSI C into use on C++ side too. However strdup does not belong to either of these standards. Instead the UNIX98 (and UNIX01/03) says that strdup is available by including <string.h> [1].

We happen to use define XOPENSOURCE=500 which means that we are using UNIX98 standard and thus we should get (and we get) strdup if we include string.h. However ANSI C++ standard says that string.h is depricated and should not be used.

So after this rather long introduction I can finally present the question why I started to write the message :-)

Is it supposed that when XOPENSOURCE=500 is used to compile standard C++ program, strdup function is available by including <cstring> header?

If not what is then the "right" way to get this strdup-function that is defined to be in UNIX98 standard into use? Remeber that ANSI C++ says that <string.h> is depricated header.

I have compiled the same program on Solaris8 with SUNPRO CC 5.5 compiler and GNU g++ 3.3.2 compiler . Also I have made compilation on HPUX 11 with HP's aCC compiler version 3.37 and further on Linux using GNU g++ 3.3.2 compiler. On all the enviroments I have XOPENSOURCE=500 defined in CXXFLAGS and the only compiler where the above problem occurs is SUNPRO CC 5.5. So is this bug in compiler (or in itse header files) or am I facing a problem to which solution is called "mismatch between standards" ;-)?

[1] http://www.opengroup.org/onlinepubs/007908799/xsh/strdup.html

--
Tuomo Tikkanen
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 27 2004
Added on Apr 23 2004
1 comment
749 views