当前位置:网站首页>strcpy_ S precautions for use. (do not use strcpy_s where memcpy_s can be used)
strcpy_ S precautions for use. (do not use strcpy_s where memcpy_s can be used)
2022-06-13 09:00:00 【xiongpursuit88】
strcpy_s() There are two versions of the function : Three parameter versions and two parameter versions , as follows :
When there are three parameters :
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
For two parameters :
errno_t strcpy_s(
char (&strDestination)[size],
const char *strSource
); // C++ only
The following focuses on three parameter versions .
Be sure to pay attention to : The second parameter numberOfElements It must be larger than the source space strSource Size . otherwise Debug The version will pop up ,release The version will crash .
such as
LPSTR l_pcDest = (char*)malloc(6);
char l_acPhyPosName[6] = { 0 };
sprintf_s(l_acPhyPosName, “HOP05”);
strcpy_s(l_pcDest, 6/ It can't be for 5, Must be greater than or equal to strlen(l_acPhyPosName)+1/, l_acPhyPosName);
however ,memcpy_s The second parameter in the needs to be greater than or equal to strlen(l_acPhyPosName).( Not greater than or equal to strlen(l_acPhyPosName)+1 Oh ) as follows :
LPSTR l_pcDest = (char*)malloc(6);
char l_acPhyPosName[6] = { 0 };
sprintf_s(l_acPhyPosName, “HOP03”);
memcpy_s(l_pcDest, 5/DestLen/, l_acPhyPosName, 5/SourceLen/);
// summary :strcpy_s And memcpy_s The difference between :
//strcpy_s Is a copy of the string , Source includes \0 Of , So the target length must be greater than or equal to strlen( Source )+1, and memcpy_s Memory considerations , As long as the target length is greater than or equal to the source length , Unwanted +1.
// therefore , To avoid confusion , It works memcpy_s The place of , Don't use strcpy_s.
边栏推荐
- Vscode define code block -- define cursor position
- Pytorch same structure different parameter name model loading weight
- 20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化
- 【 sécurité 】 comment devenir ingénieur de sécurité de 0 à 1 contre - attaque pour la Fondation zéro
- 20211028 调节和跟踪
- Qvector shallow copy performance test
- Opencv gaussianblur() explanation (Sigma value)
- Implement authentication code login and remember password (cookie)
- Onnx crop intermediate node
- What are the bank financial products? How long is the liquidation period?
猜你喜欢
Use of grep
Installation of sonarqube code quality management platform (to be continued)
Message Oriented Middleware
【安全】零基础如何从0到1逆袭成为安全工程师
Tutorial (5.0) 03 Security policy * fortiedr * Fortinet network security expert NSE 5
redis
DIY UAV (anonymous controller p2+f330 rack)
On the use of regular expressions (bracket problem)
20220606 关于矩阵的Young不等式
Simulink如何添加模块到Library Browser
随机推荐
Screenshot of cesium implementation scenario
torch. How to calculate addmm (m, mat1, mat2)
A solution to create a new EXCEL workbook on win10 computer and change the suffix to xlsm (normally it should be xlsx)
Gbase 8A v95 vs v86 compression strategy analogy
顺时针打印个数组
关于RSA加密解密原理
JS array method
Is it safe to open an account online? Can a novice open an account?
Cesium view switching, locating, reading files, building data sources, entity control, model control, etc
Knowledge points related to system architecture 3
ES6 module import / export summary
【QNX Hypervisor 2.2 用户手册】4.5 构建Guest
JS obtain geographic location information according to longitude and latitude and mark it on the map
DIY UAV (anonymous controller p2+f330 rack)
Brief description of port, domain communication port and domain service
20211028 Stabilizability
Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
Four kinds of hooks in deep learning
Sonar scan ignores the specified file
Installation of sonarqube code quality management platform (to be continued)