C language memcpy and memcpy_s difference - C language zero foundation tutorial

catalogue

Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language

I Memcpy and memcpy_s function difference

1. Grammar comparison

memcpy_s function syntax

/*
*Description: this type of function is used to copy (copy) a string.
*
*Parameters:
*   [out] strDestination: String after copy
*   [in] numberOfElements:  strDestination Target buffer length
*   [in] strSource: String to copy
*   [in] count: Length of string to be copied
*
*Return value: an integer is returned. 0 means the copy is successful, bai non-0 means the copy is unsuccessful, and different values mean different errors. For details, please refer to the MSDN manual
*Note: numberofelements must be greater than or equal to count, otherwise the copy will be interrupted.
*/
errno_t memcpy_s(char *strDestination , size_t numberOfElements , const char *strSource , size_t count);

memcpy function syntax

/*
*Description: this kind of function is used to copy (copy) the string. It belongs to memory copy!
*
*Parameters:
*   [out] destin: String after copy
*   [in] source: String to copy
*   [in] n: Number of bytes to copy
*
*Return value: pointer to the string destin ation
*Note: if the number of bytes to be copied n is greater than the memory size of destin ation, the program will crash
*/
void *memcpy(void *destin, void *source, unsigned n);

2.memcpy and memcpy_s same point

C language Memcpy function in / memcpy_s function, can be used to complete char string Memory copy; Memcpy function / memcpy_s functions belong to memory copy, so even if \ 0 is encountered during the copy process, it will not end strcpy function / strcpy_s function It belongs to string copy. If you encounter \ 0 during the copy process, it will end immediately;

3.memcpy and memcpy_s differences

use memcpy_s function It is safer than the memcpy function. We also noticed two problems:

1. Error in memcpy function: error C4996

error C4996: 'memcpy': This function or variable may be unsafe. Consider using memcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

terms of settlement: include Add code after
#pragma warning( disable : 4996)

2. The memcpy function has no method to ensure the effective buffer size, which is unsafe to use

memcpy function There is 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. During program execution, this will lead to unpredictable behavior and easily lead to program crash, such as the following code:

char src[1024] = { "C/C++course-memcpy 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("memcpy before dst:%s\n", dst);
memcpy(dst, src , len_src);  // Obviously, the space size of dst cannot completely store src, and the program will crash
printf("memcpy after dst:%s\n", dst);

III Memcpy and memcpy_s function practice

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com com
//@File:C language tutorial - C language memcpy and memcpy_s difference
//@Time:2021/06/06 08:00
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/

#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
//error C4996: 'memcpy': This function or variable may be unsafe. Consider using memcpy_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++course-memcpy_s function - www.codersrc.com" };
    char dst[1024] = { 0 };
    char dst_new[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);
    //Using memcpy_s function
    printf("memcpy_s before dst:%s\n", dst);
    memcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src , sizeof(src)/sizeof(src[0]));
    printf("memcpy_s after dst:%s\n", dst);
    //Using the memcpy function
    printf("memcpy before dst_new:%s\n", dst_new);
    memcpy(dst_new, src , sizeof(src)/sizeof(src[0]));
    printf("memcpy after dst_new:%s\n", dst_new);
    printf("\n");
    system("pause");
}
/*
Output:
len_src:1024 len_dst:1024
memcpy_s Previous dst:
memcpy_s After dst:C/C + + tutorial - memcpy_s function - www.codersrc com
memcpy Previous dst_new:
memcpy After dst_new:C/C + + tutorial - memcpy_s function - www.codersrc com
 Please press any key to continue
*/

be careful:

  • 1. memcpy_ The second parameter numberofelements of the s function is to set the size of the target buffer, which is related to the target buffer

  • 2. memcpy_ The fourth parameter count of the s function is to set the size of the data to be copied, which is related to the original buffer

  • 3. memcpy_ The second argument numberOfElenments of the s function must be greater than or equal to memcpy_ The fourth parameter of the s function is count, otherwise the copy will be interrupted

    memcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src,sizeof(src)/sizeof(src[0])); // Correct writing
    memcpy_s(dst, sizeof(src)/sizeof(src[0]), src,sizeof(src)/sizeof(src[0])); // Wrong writing
    memcpy_s(dst, sizeof(dst)/sizeof(dst[0]), src,sizeof(dst)/sizeof(dst[0])); // Wrong writing
    memcpy_s(dst, sizeof(src)/sizeof(src[0]), src,sizeof(dst)/sizeof(dst[0])); // Wrong writing

IV 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 Visual Studio font / background / line number
  6. C language format controller / placeholder
  7. C language logical operators
  8. C language ternary operator
  9. C language comma expression
  10. C language self addition and self subtraction operator (+ + i / i + +)
  11. C language for loop
  12. C language break and continue
  13. C language while loop
  14. C language do while and while loop
  15. C language switch statement
  16. C language goto statement
  17. C language char string
  18. C language strlen function
  19. sizeof function in C language
  20. Differences between sizeof and strlen functions in C language
  21. C language strcpy function
  22. C language strcpy_s function
  23. C language strcpy and strcpy_s function difference
  24. C language memcpy and memcpy_s difference

No reprint without permission: Ape programming » C language memcpy and memcpy_s difference

Added by renojim on Thu, 23 Dec 2021 16:59:00 +0200