Jump to content
  • Advertisement
Sign in to follow this  
derek7

Are there safe string copy function?

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I find the strcpy strcat etc function is not safe,so I must wrtie safe function ? I know maybe stl string work.but in c no good string function?

Share this post


Link to post
Share on other sites
Advertisement
Um... You mean like strncpy and strncat that take an extra parameter that specifies maximum count of bytes copied?

Or do you by "safe" mean something other?

Oxyd

Share this post


Link to post
Share on other sites
Strncpy and strncat could be considered safe, but saying that their actual behaviour is unintuitive would be an understatement. C99 brings a few useful new functions too (such as snprintf, with proper termination).
Just be sure to study the documentation and you can use them in a safe way.

If you intend to use C-strings then I suggest writing your own set of wrapper functions for the most tedious tasks. At least that's should prove a good way to learn how to handle them.

Share this post


Link to post
Share on other sites


std::string source("Hello");
std::string copy = source;//copy a string
copy+=", World!";//concatenate a string
copy+=copy;



etc.

Share this post


Link to post
Share on other sites
I'd advise using std::string if possible, but if that isn't possible (e.g., you are maintaining code that uses char* extensively), the new release (Whidbey) of Visual C++ contains a Secure CRT (C Runtime Library), which exports a few more string functions.

The new functions are designed to check output buffer sizes, etc., so that arrays are not overwritten. The new functions are strcpy_s, strcat_s, tmpnam_s, etc.

The new functions have been submitted to the standards committee for review, so they may appear in other compilers at a later stage.

Refer to this msdn blog for more info.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!