This is the 12th day of my participation in the August Text Challenge.More challenges in August

directory

  • Strcpy_s function introduction
    • 1. Strcpy function error C4996
    • 2. The strcpy function has no method to ensure a valid buffer size and is not safe to use
  • Strcpy_s function syntax
  • Strcpy_s function combat
    • Strcpy_s function is simple to use
    • 2. Strcpy_s ends with ‘\0’
  • Four. Guess you like it

C/C++ learning Directory >> C language basics

Strcpy_s function introduction

C language strcpy function in string. H, can be used to complete the char string copy; The strcpy_s function is similar to the strcpy function. When strcpy is used, we also notice two problems:

1. Strcpy function error C4996

For details, see: C error C4996: This Function or Variable May be unsafe

error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. #pragma Warning (disable: 4996)Copy the code

2. The strcpy function has no method to ensure a valid buffer size and is not safe to use

Strcpy_s is a system security function. After 2005, Microsoft suggested to use a so-called system security function, strcpy_s replaced Strcpy. The strcpy function has no way to guarantee a valid buffer size, so it can only assume that the buffer is large enough to hold the string to be copied. When the program executes, this causes unpredictable behavior that can easily cause the program to crash, such as the following code:

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language strcpy_S function // @time :2021/06/03 08:00 // @motto: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ char src[1024] = { "C/C++ tutorial -strcpy function \0 - www.codersrc.com"}; char dst[10] = { 0 }; int len_src = sizeof(src)/sizeof(char); // 1024 int len_dst = sizeof(dst) / sizeof(char); //10 printf("len_src:%d len_dst:%d\n", len_src,len_dst); Printf ("strcpy before DST :%s\n", DST); strcpy(dst, src); Printf (" after strcpy DST :%s\n", DST);Copy the code

Strcpy_s function syntax

The strcpy_s function can avoid the above unexpected behavior by setting the target buffer size as follows:

/* * Description: This type of function is used to copy (copy) strings. * * Parameters: * [out] strDestination: string after copy * [in] numberOfElements: strDestination Destination buffer length * [in] strSource: String * * to be copied Returns an integer, 0 for successful replication, non-0 for unsuccessful replication, different values for different errors, Errno_t strcpY_s (char *strDestination, size_t numberOfElements, const char *strSource); errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);Copy the code

Strcpy_s function combat

Strcpy_s function is simple to use

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language strcpy_S function // @time :2021/06/03 08:00 // @motto: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ #include "stdafx.h" #include<stdlib.h> #include<stdio.h> #include<string.h> #include "windows.h" //error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. //#pragma warning( disable : 4996) void main() {char SRC [1024] = {"C/C++ tutorial -strcpy_s function - www.codersrc.com"}; char dst[1024] = { 0 }; int len_src = sizeof(src)/sizeof(char); int len_dst = sizeof(dst) / sizeof(char); printf("len_src:%d len_dst:%d\n", len_src,len_dst); Printf ("strcpy_s before DST :%s\n", DST); strcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src); Printf ("strcpy_s after DST :%s\n", DST); printf("\n"); system("pause"); } /* Output: len_src:1024 len_dst:1024 strcpy_s before DST: strcpy_s after DST :C/C++ tutorial -strcpy_s function - www.codersrc.com Please press any key to continue.. */Copy the code

Note: the second argument to strcpy_s is to set the target buffer size, not the original buffer size

strcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src); Strcpy_s (DST, sizeof(SRC)/sizeof(SRC [0]), SRC); // ErrorCopy the code

2. Strcpy_s ends with ‘\0’

Char char char char char char char char char char char char char

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language strcpy_S function // @time :2021/06/03 08:00 // @motto: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ char src[1024] = { "C/C++ tutorial -strcpy_s function \0 - www.codersrc.com"}; char dst[1024] = { 0 }; Printf ("strcpy_s before DST :%s\n", DST); strcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src); Printf ("strcpy_s after DST :%s\n", DST); printf("\n"); system("pause"); /* Output: strcpy_s before DST: strcpy_s after DST :C/C++ tutorial -strcpy_s function please press any key to continue.. */Copy the code

Strcpy_s (‘\0’, DST) is missing a string “-www.codersrc.com “;

Four. Guess you like it

  1. Install Visual Studio
  2. Install the Visual Studio plug-in, Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 Uninstall
  5. Set the Visual Studio font/background/line number
  6. C format controller/placeholder
  7. C language logic operator
  8. C language ternary operator
  9. C language comma expression
  10. C (++ I/I ++)
  11. C language for loop
  12. C language break and continue
  13. C while loop
  14. C does while and while loops
  15. C switch statement
  16. C language GOto statement
  17. C char Character string
  18. C strlen function
  19. C sizeof
  20. C sizeof and strlen are different
  21. C language strcpy function
  22. C language strcpy_s function

C strcpy_s function

This article is published by the blog – Ape Say Programming Ape Say programming!