[STL] strcpy and strcpy_s in C ++ cstring Library

Source: Internet
Author: User
// Strcpy usage: // 1. If the source length <target length ---> OK. // Copy the source string to the target string (including the ending character), and keep other bytes unchanged. // 2. If the source length is greater than the target length, an error is returned. // Copy the source string to the target string (including the terminator). However, the target string is too short and the subscript exceeded, abnormal strings will obviously cause exceptions during runtime // usage principle Summary: Ensure the source length during use <target length !! Void test_strcpy () {char strsource [] = "sample string"; char strdestshort [10]; char strdestlong [40]; int isource = sizeof (strsource ); int idestshort = sizeof (strdestshort); int idestlong = sizeof (strdestlong); // 1if (isource <idestlong) strcpy (strdestlong, strsource); STD :: cout <strdestlong <STD: Endl; // 2if (isource> idestshort) {// strcpy (strdestshort, strsource ); in this way, an exception will be reported // it will be handled in the most traditional way. Of course, it is better to use the standard library function size_t L = sizeof (strdestshort); For (size_t I (0 ); I <L-1; ++ I) strdestshort [I] = strsource [I]; strdestshort [L-1] = 0;} STD :: cout <strdestshort <STD: Endl;} // strcpy_s usage: // if the source length is <target length sizeof ([min, Max]) ---> OK, however, when the length of a custom copy string exceeds the target length, a running exception may occur because the subscript is out of the range. // if the source length is greater than the target length, sizeof may run abnormally, // (it may be that the compiler checks the length of the two before copying. If the source string is long, an alarm is triggered and the system exits. Therefore, sizeof (strlen (strdest) = 0) is used later) statement is invalid) // usage principle Summary: // 1. ensure: Source length <target length // 2. the length cannot exceed szdest. We recommend that you use sizeof (szdest) void test_strcpy_s () {char strsource [] = "sample string"; char strdestshort [10]; char strdestlong [40]; int isource = sizeof (strsource); int idestshort = sizeof (strdestshort); int idestlong = sizeof (strdestlong); // 1if (isource <idestlong) limit (strdestlong, idestlong, strsource); STD: cout <strdestlong <STD: Endl; // 2if (isource> idestshort) strcpy_s (strdestshort, idestshort, strsource ); // This will report an exception STD: cout <strdestshort <STD: Endl ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.