This is the 12th day of my participation in the August Text Challenge.More challenges in August

directory

  • Memcpy function introduction
  • Two. Memcpy function combat
    • 1. Simple use of memcpy function
    • 2. Strcpy is a string copy
    • 3. The memcpy function is a memory copy
    • 4. Note the crash of the memcpy function
  • Guess you like it

C/C++ learning Directory >> C language basics

Memcpy function introduction

Char char char char char char char char char char char char char char

/* * description: this type of function is used to copy a string (copy), belongs to memory copy! * * Parameters: * [out] DST: string to be copied * [in] SRC: string to be copied * [in] n: number of bytes to be copied * * Return value: pointer to DST * Note: */ void *memcpy(void * DST, void * SRC, unsigned int n); */ void *memcpy(void * DST, void * SRC, unsigned int n);Copy the code

Note:

1.strcpyFunctions andstrcpy_sFunction in the copy process, if encountered'\ 0'End character, then directly end the copy;The memcpy functionEven in the process of copying'\ 0'Endings do not end;

The strcpy and strcpy_s functions are string copies; Memcpy functions are memory copies;Copy the code

2. If the memcpy function displays error: 4996, refer to error C4996: ‘fopen’ : This function or variable may be unsafe

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.
Copy the code

3. Ensure that the DST memory space is large enough to accommodate SRC. If the DST memory space is smaller than SRC, an overflow error will occur and the program will crash. For example, a 50ml glass of water will fit into a 500ml glass of water. A 500ml glass of water will overflow into a 50ml glass of water.

Two. Memcpy function combat

1. Simple use of memcpy function

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language memcpy function // @time :2021/06/03 08:00 // @motto: short step without a thousand miles, not small streams without rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include "stdafx.h" #include<stdlib.h> #include<stdio.h> #include<string.h> #include "windows.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++ tutorial -memcpy function - www.codersrc.com"}; char dst[1024] = { 0 }; Printf (" before memcpy DST :%s\n", DST); // empty string memcpy(DST, SRC, sizeof(SRC)/sizeof(char)); Printf (" after memcpy DST :%s\n", DST); // printf("\n"); system("pause"); } /* Output: memcpy before DST: memcpy after DST :C/C++ tutorial -memcpy function - www.codersrc.com Please press any key to continue... */Copy the code

2. Strcpy is a string copy

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language memcpy function // @time :2021/06/03 08:00 // @motto: short step without a thousand miles, not small streams without rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ char src[1024] = { "C/C++ tutorial -strcpy function \0 - www.codersrc.com"}; char dst[1024] = { 0 }; Printf ("strcpy before DST :%s\n", DST); strcpy(dst, src ); Printf ("strcpy after DST :%s\n", DST); printf("\n"); system("pause"); /* Output: strcpy before DST: strcpy after DST :C/C++ tutorial -strcpy function please press any key to continue.. */Copy the code

If the strcpy function is copied with ‘\0’, the copy will end immediately, so the strcpy string is missing a string “-www.codersrc.com”.

3. The memcpy function is a memory copy

The memcpy function, however, is a copy of memory. Even if a ‘\0’ terminator is encountered during the copy process, the copy will not end, for example:

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language memcpy function // @time :2021/06/03 08:00 // @motto: short step without a thousand miles, not small streams without rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ char src[1024] = { "C/C++ tutorial -memcpy function \0 - www.codersrc.com"}; char dst[1024] = { 0 }; Printf (" before memcpy DST :%s\n", DST); memcpy(dst, src, sizeof(src)/sizeof(char)); Printf (" after memcpy DST :%s\n", DST); printf("\n"); system("pause"); /* Output: memcpy before DST: memcpy after DST :C/C++ tutorial -memcpy function \0 - www.codersrc.com Please press any key to continue.. */Copy the code

When memcpy functions are copied in memory, ‘\0’ is used only as data in memory, and does not represent the end of the copy.

4. Note the crash of the memcpy function

The memcpy function does not check the size of two strings when copying a string. This is one of the causes of error C4996. For example:

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language memcpy function // @time :2021/06/03 08:00 // @motto: short step without a thousand miles, not small streams without rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include "stdafx.h" #include<stdlib.h> #include<stdio.h> #include<string.h> #include "windows.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++ tutorial -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 (" before memcpy DST :%s\n", DST); memcpy(dst, src, len_src); Printf (" after memcpy DST :%s\n", DST); } /* Output: len_src:1024 len_dst:10 */Copy the code

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 the Visual Studio font/background/line number
  6. C format controller/placeholder
  7. C language logic operator
  8. C language ternary operator
  9. C language comma expression
  10. C (++ I/I ++)
  11. C language for loop
  12. C language break and continue
  13. C while loop
  14. C does while and while loops
  15. C switch statement
  16. C language GOto statement
  17. C char Character string
  18. C strlen function
  19. C sizeof
  20. C sizeof and strlen are different
  21. C language strcpy function
  22. C language strcpy_s function
  23. C language memcpy function

C language memcpy function

This article is published by the blog – Ape Say Programming Ape Say programming!