当前位置:网站首页>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.
边栏推荐
- Cesium displays a pop-up box at the specified position and moves with the map
- redis 模糊查询 批量删除
- 消息中间件
- [network security] webshell empowerment of new thinking of SQL injection
- Calculation method of paging
- MySQL startup error: innodb: operating system error number 13 in a file operation
- I set up a blog
- A solution to create a new EXCEL workbook on win10 computer and change the suffix to xlsm (normally it should be xlsx)
- How to become a white hat hacker? I suggest you start from these stages
- 20211020 段院士全驱系统
猜你喜欢

消息中间件

À propos des principes de chiffrement et de décryptage RSA

0. some doubts about learning SolidWorks for the first time

Docker installing MySQL local remote connection docker container MySQL

Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5

useRoutes() may be used only in the context of a <Router> component.

VI editor

Use of grep

Uni app subcontracting loading and optimization

Browser render passes
随机推荐
Opencv gaussianblur() explanation (Sigma value)
Visual studio tools using shortcut keys (continuous update)
Problems in the deconstruction and assignment of objects, comparison between empty strings and undefined
Can I open an account for the reverse repurchase of treasury bonds? Can I directly open the security of securities companies on the app for the reverse repurchase of treasury bonds? How can I open an
Diversified tables through TL table row consolidation
The Jenkins console does not output custom shell execution logs
Browser render passes
pytorch相同结构不同参数名模型加载权重
How does jupyter notebook directly output the values of multiple variables after running?
JS string method
国债逆回购能开户吗,国债逆回购在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
20220524 如何把CoppeliaSim安装到D盘
5. Attribute selector
Vscode plug in
torch. How to calculate addmm (m, mat1, mat2)
How excel adds hyperlinks to some text in a cell
Uni app subcontracting loading and optimization
Bash: kill: (74001) - operation not allowed
Redirect vulnerability analysis of network security vulnerability analysis
ES6 use of dynamic attributes