strcpy_s and strcpy() Functions function almost the same .strcpy function . Just like gets The function is the same , It has no way to guarantee an effective buffer size , So it can only assume that the buffer is large enough to hold the string to be copied . When the program is executed , This will lead to unpredictable behavior .

use strcpy_s We can avoid these unexpected behaviors .

This function takes two arguments 、 All three parameters can , Just be able to guarantee the buffer size .

Three parameters :

errno_t strcpy_s(

char *strDestination,

size_t numberOfElements,

const char *strSource

);

Two parameters :

errno_t strcpy_s(

char (&strDestination)[size],

const char *strSource

); // C++ only



Examples :

#include<iostream>
#include<cstring>
using namespace std;

void Test(void)
{
char *str1=NULL;
str1=new char[20];
char str[7];
strcpy_s(str1,20,"hello world");// Three parameters
strcpy_s(str,"hello");// Two parameters, but suppose :char *str=new char[7]; Will make mistakes : Prompt does not support two parameters
cout<<"strlen(str1):"<<strlen(str1)<<endl<<"strlen(str):"<<strlen(str)<<endl;
printf(str1);
printf("\n");
cout<<str<<endl;
}

int main()
{
Test();
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;

void Test(void)
{
char *str1=NULL;
str1=new char[20];
char str[7];
strcpy_s(str1,20,"hello world");// Three parameters
strcpy_s(str,"hello");// Two parameters, but suppose :char *str=new char[7]; Will make mistakes : Prompt does not support two parameters
cout<<"strlen(str1):"<<strlen(str1)<<endl<<"strlen(str):"<<strlen(str)<<endl;
printf(str1);
printf("\n");
cout<<str<<endl;
}

int main()
{
Test();
return 0;
}

Output is :

strlen(str1): 11        // In addition, pay attention to :strlen(str1) Is to calculate the length of a string . Does not contain... At the end of the string “\0”!!!

strlen(str): 5

hello world

hello

strcpy_s And strcpy More related articles

  1. strcpy_s And strcpy The comparison of

    strcpy_s and strcpy() The function is almost the same .strcpy function , Just like gets The function is the same , It has no way to guarantee an effective buffer size , So it can only assume that the buffer is large enough to hold the string to be copied . When the program is executed , This will ...

  2. strcpy_s and strcpy()

    from : https://www.cnblogs.com/hrhguanli/p/4570093.html strcpy_s and strcpy() Functions function almost the same .strcpy function . Just like gets The function is the same , it ...

  3. strcpy_s And strcpy The difference between

    strcpy_s and strcpy() The function functions are almost the same .];];strcpy_s(str1,,;}#include<iostream>  #include<]; ]; strcpy ...

  4. Yes strcpy_s Several tests

    I found out today that if strcpy This function , The goal is buffer Too small , There will be unexpected collapses . And it's not easy to debug . Try to use it in the future strcpy_s 了 . strcpy_s yes strcpy A more secure version of 1. When the target string parameter is a character ...

  5. C++string Function strcpy_s

    strcpy_s and strcpy() The function functions are almost the same .strcpy function , Just like gets The function is the same , It has no way to guarantee an effective buffer size , So it can only assume that the buffer is large enough to hold the string to be copied . When the program is running , This will lead to ...

  6. strcpy and memcpy The difference between

    strcpy and memcpy Are standard C Library function . They have the following characteristics . strcpy Provides a copy of a string . namely strcpy Just for string copying . And it's not just copying the contents of the string , It also copies the end of the string ,strcpy_ ...

  7. strcpy_s Function usage

    strcpy_s and strcpy() The function functions are almost the same . strcpy function , Just like gets The function is the same , It has no way to guarantee an effective buffer size , So it can only assume that the buffer is large enough to hold the string to be copied . When the program is running , This will lead to ...

  8. C++ Based on learning -20120516

    1. Here's how to use strcpy_s And strcpy Security comparison of char szBuf[2] = {0}; strcpy_s(szBuf, 2, "12131");  // new CRT function   ...

  9. wcscpy wcscpy_s strcpy strcpy_s The difference between

    Prototype declaration :extern char *strcpy(char *dest,const char *src); The header file :string.h function : Take from src Address starts with and contains NULL The string of the terminator is assigned to des ...

Random recommendation

  1. java Log framework slf4j And log4j

    Logging is naturally very important , But I'm afraid I can remember slf4j And log4j Few people wait for the log framework to be configured , It's not hard , It's just that it's rarely moved after it's configured , Developing new projects is usually copied from other projects , Or refer to the documentation I don't say much nonsense , First say log4 ...

  2. springMVC Create base variables

    stay springMVC Some of the variables in are base variables , It can be used in many places in the whole world , When changing the rules , It's just a matter of changing one place , And it can avoid a lot of unnecessary bug Now let's summarize how to create a global basic change in my project ...

  3. NYOJ subject 65 Another factorial problem

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsIAAAJhCAIAAADE+EHOAAAgAElEQVR4nO3drXLjSAMu4HMT4bmQYF

  4. C# Modify file permissions

    The format of the user name is :Local MachineName\AccountName The machine name can be accessed through System.Environment.MachineName obtain . Get permission to a file ( Account number ) list FileSe ...

  5. windows Infiltrate some neglected little orders

    cmdkey /list You can list the available credentials between intranets . wmic   useraccount get name,sid gpresult /r

  6. Webpack 4 course - The sixth part Enhance the development time experience

    Reprint please indicate the source : Grape city official website , Grape city provides professional development tools for developers . Solutions and services , Enabling developers . The source of the original text is :https://wanago.io/2018/08/06/webpack-4-course-part ...

  7. docker Container access host IP

    Host execution ifconfig  Will see docker0 that ip, You can use the visiting host

  8. XML Namespace (xmlns) attribute

    http://www.w3school.com.cn/xml/xml_namespaces.asp XML Namespace (xmlns) attribute XML The namespace attribute is placed in the start tag of the element , and ...

  9. Asp.Net Mvc Custom extension

    Catalog : Custom model IModelBinder Custom model validation Custom view engine Customize Html Auxiliary method Customize Razor Auxiliary method Customize Ajax Auxiliary method Custom controller extensions Custom filter Customize Action ...

  10. Kafka:ZK+Kafka+Spark Streaming Cluster environment construction ( twenty-seven ):kafka manager install

    One .kafka-manager brief introduction To simplify maintenance for developers and service engineers Kafka The work of clustering ,yahoo We built a system called Kafka The manager is based on Web Tools , be called Kafka Manager. This management tool can accommodate ...