目錄
- 一.memcpy 函式簡介
- 二.memcpy 函式實戰
- 1.memcpy 函式簡單使用
- 2.strcpy 函式屬于字串拷貝
- 3.memcpy 函式屬于記憶體拷貝
- 4.memcpy 函式注意崩潰問題
- 三.猜你喜歡
零基礎 C/C++ 學習路線推薦 : C/C++ 學習目錄 >> C 語言基礎入門
一.memcpy 函式簡介
C 語言在 string.h 中 strcpy 函式和 strcpy_s 函式,可用完成 char 字串拷貝,對于字串拷貝,還有 memcpy 函式也能完成,語法如下:
/*
*描述:此類函式是用于對字串進行復制(拷貝),屬于記憶體拷貝!
*
*引數:
* [out] dst:拷貝完成之后的字串
* [in] src :需要拷貝的字串
* [in] n :需要拷貝的位元組數
*
*回傳值:指向 dst 這個字串的指標
*注意:如果需要拷貝的位元組數n 大于 dst 的記憶體大小,程式會崩潰
*/
void *memcpy(void *dst, void *src, unsigned int n);
注意:
1.strcpy 函式和 strcpy_s 函式在拷貝程序中,如果遇到'\0'結束符,那么直接結束拷貝;memcpy 函式拷貝程序中就算遇到'\0'結束符也不會結束;
strcpy 函式和 strcpy_s 函式 屬于字串拷貝;
memcpy 函式屬于記憶體拷貝;
2.如果使用 memcpy 函式提示 error:4996,請參考: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.
3.必須保證 dst 空間足夠大,能夠容納 src ,如果 dst 記憶體空間大小比 src 更小,會導致溢位錯誤,引起程式崩潰!可以通過 sizeof 函式查看記憶體記憶體大小,舉個例子:50ml 的水杯能倒進 500ml 的水杯沒問題,500ml 的水杯倒進 50ml 的水杯,會溢位很多水;
二.memcpy 函式實戰
1.memcpy 函式簡單使用
/******************************************************************************************/
//@Author:猿說編程
//@Blog(個人博客地址): www.codersrc.com
//@File:C語言教程 - C語言 memcpy 函式
//@Time:2021/06/03 08:00
//@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
/******************************************************************************************/
#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++教程-memcpy函式 - www.codersrc.com" };
char dst[1024] = { 0 };
printf("memcpy之前 dst:%s\n", dst); //空字串
memcpy(dst, src , sizeof(src)/sizeof(char));
printf("memcpy之后 dst:%s\n", dst);//
printf("\n");
system("pause");
}
/*
輸出:
memcpy之前 dst:
memcpy之后 dst:C/C++教程-memcpy函式 - www.codersrc.com
請按任意鍵繼續. . .
*/
2.strcpy 函式屬于字串拷貝
在 char 字串中有作介紹,字串默認都是'\0'結尾,strcpy 函式或者 strcpy_s 函式在拷貝程序中,如果遇到'\0'結束符,那么直接結束拷貝,看下面例子:
/******************************************************************************************/
//@Author:猿說編程
//@Blog(個人博客地址): www.codersrc.com
//@File:C語言教程 - C語言 memcpy函式
//@Time:2021/06/03 08:00
//@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
/******************************************************************************************/
char src[1024] = { "C/C++教程-strcpy函式\0 - www.codersrc.com" };
char dst[1024] = { 0 };
printf("strcpy之前 dst:%s\n", dst);
strcpy(dst, src );
printf("strcpy之后 dst:%s\n", dst);
printf("\n");
system("pause");
/*
輸出:
strcpy之前 dst:
strcpy之后 dst:C/C++教程-strcpy函式
請按任意鍵繼續. . .
*/
重上面的輸出結果可以看出:strcpy 函式在拷貝的時候,如果遇到'\0',那么拷貝直接結束,所以上面使用 strcpy 拷貝的時候,dst 字串明顯少了一段字符" - www.codersrc.com";
3.memcpy 函式屬于記憶體拷貝
而 memcpy 函式不同,memcpy 屬于記憶體拷貝,即便在拷貝程序中遇到'\0'結束符,也不會結束拷貝,舉個例子:
/******************************************************************************************/
//@Author:猿說編程
//@Blog(個人博客地址): www.codersrc.com
//@File:C語言教程 - C語言 memcpy函式
//@Time:2021/06/03 08:00
//@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
/******************************************************************************************/
char src[1024] = { "C/C++教程-memcpy函式\0 - www.codersrc.com" };
char dst[1024] = { 0 };
printf("memcpy之前 dst:%s\n", dst);
memcpy(dst, src, sizeof(src)/sizeof(char));
printf("memcpy之后 dst:%s\n", dst);
printf("\n");
system("pause");
/*
輸出:
memcpy之前 dst:
memcpy之后 dst:C/C++教程-memcpy函式\0 - www.codersrc.com
請按任意鍵繼續. . .
*/
很明顯,memcpy 函式記憶體拷貝的時候,'\0'僅僅是當作了記憶體中的資料,并不代表拷貝結尾;
4.memcpy 函式注意崩潰問題
如果使用 memcpy 的時候需要拷貝的字符大小比 dst 記憶體大時,程式運行會崩潰,memcpy 函式在字串拷貝的時候并不會檢查兩個字串空間大小,所有很容易產生崩潰,這也是 error C4996 的原因之一,舉個例子:
/******************************************************************************************/
//@Author:猿說編程
//@Blog(個人博客地址): www.codersrc.com
//@File:C語言教程 - C語言 memcpy函式
//@Time:2021/06/03 08:00
//@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
/******************************************************************************************/
#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++教程-memcpy函式\0 - www.codersrc.com" };
char dst[10] = { 0 };
int len_src = https://www.cnblogs.com/shuopython/p/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之前 dst:%s\n", dst);
memcpy(dst, src, len_src); // 很明顯 dst 的空間大小只有10個位元組并不能完全存放 src 1024個位元組,程式崩潰
printf("memcpy之后 dst:%s\n", dst);
}
/*
輸出:
len_src:1024 len_dst:10
*/
三.猜你喜歡
- 安裝 Visual Studio
- 安裝 Visual Studio 插件 Visual Assist
- Visual Studio 2008 卸載
- Visual Studio 2003/2015 卸載
- 設定 Visual Studio 字體/背景/行號
- C 語言格式控制符/占位符
- C 語言邏輯運算子
- C 語言三目運算子
- C 語言逗號運算式
- C 語言自加自減運算子(++i / i++)
- C 語言 for 回圈
- C 語言 break 和 continue
- C 語言 while 回圈
- C 語言 do while 和 while 回圈
- C 語言 switch 陳述句
- C 語言 goto 陳述句
- C 語言 char 字串
- C 語言 strlen 函式
- C 語言 sizeof 函式
- C 語言 sizeof 和 strlen 函式區別
- C 語言 strcpy 函式
- C 語言 strcpy_s 函式
- C 語言 memcpy 函式
未經允許不得轉載:猿說編程 ? C 語言 memcpy 函式
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/342988.html
標籤:C
