当前位置:网站首页>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.
边栏推荐
- Uni app essay
- Pytorch same structure different parameter name model loading weight
- 【 sécurité 】 comment devenir ingénieur de sécurité de 0 à 1 contre - attaque pour la Fondation zéro
- Yarn package management tool
- MySQL 8.0 modifying SQL_ mode=only_ full_ group_ by
- 20211005 Hermite矩阵及几个性质
- transforms. ColorJitter(0.3, 0, 0, 0)
- [network security] webshell empowerment of new thinking of SQL injection
- ADT Google browser plug-in ad Terminator
- MySQL startup error: innodb: operating system error number 13 in a file operation
猜你喜欢
[security] how to counter attack from 0 to 1 to become a security engineer with zero Foundation
Tutorial (5.0) 03 Security policy * fortiedr * Fortinet network security expert NSE 5
Message Oriented Middleware
如何成为白帽子黑客?我建议你从这几个阶段开始学习
Mapbox usage, including drawing, loading, modifying, deleting points and faces, displaying pop ups, etc
Animation through svg
Onnx crop intermediate node
【安全】零基礎如何從0到1逆襲成為安全工程師
How many TCP connections can a machine create at most?
Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
随机推荐
Message Oriented Middleware
Redis分布式集群搭建
Screenshot of cesium implementation scenario
torch. How to calculate addmm (m, mat1, mat2)
transforms. ColorJitter(0.3, 0, 0, 0)
Qvector shallow copy performance test
A very detailed blog about the implementation of bilinear interpolation by opencv
【QNX Hypervisor 2.2 用户手册】4.5 构建Guest
File upload JS
Object in ES6 Use of entries()
DIY UAV (anonymous controller p2+f330 rack)
Uni app subcontracting loading and optimization
Opencv gaussianblur() explanation (Sigma value)
Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
20211006 线性变换
useRoutes() may be used only in the context of a <Router> component.
VBA format word (page, paragraph, table, picture)
redis 模糊查询 批量删除
Cesium achieves sunny, rainy, foggy, snowy and other effects
银行理财产品有哪些?清算期是多长?