全景网站制作,网站加速器,缩短网址生成器,微信上怎么创建公众号相比于内存函数#xff0c;字符串函数和字符函数是对字符串和字符进行操作#xff0c;内存函数是对内存进行操的。下面跟大家分享我学到的几个内存函数。
memcpy函数
void* memcpy(void* dest, const void* sour, size_t num);
dest是目标地址#xff0c;sour要拷贝的源地…相比于内存函数字符串函数和字符函数是对字符串和字符进行操作内存函数是对内存进行操的。下面跟大家分享我学到的几个内存函数。
memcpy函数
void* memcpy(void* dest, const void* sour, size_t num);
dest是目标地址sour要拷贝的源地址num是要拷贝的字节数
因为内存函数是对内存进行操作如果对内存进行操作就不用受制于数据的类型所以这里使用void类型的指针因为void可以接受任何类型的指针便于操作。
memcpy函数的作用是从source的位置开始向后复制num个字节的数据到dest指向的内存位置当遇到\0的时候停下来如果source和dest在内存中的地址有重叠复制的结果是未知的。
memcpy函数的使用
int main()
{int arr1[100] { 0 };int arr2[100] { 1,2,3,4,5 };memcpy(arr1, arr2, 20);//一个int类型是4个字节for (int i 0; i 5; i){printf(%d , arr1[i]);//输出结果为1 2 3 4 5}return 0;
}
memcpy函数的模拟
void* my_memcpy(void* dest, const void* sour, size_t num)
{char* ret dest;//首先存一下dest的首地址//这里为了提高模拟度因为在库函数里memcpy函数返回值就是目标函数的首地址assert(dest ! NULL);assert(dest ! NULL);int count num;//因为是对内存进行操作所以需要一个字节一个字节的交换while (count--){*((char*)dest) *((char*)sour);(char*)dest ((char*)dest) 1;(char*)sour ((char*)sour) 1;}return ret;
}
memmove函数
void * memmove ( void * dest, const void * sour, size_t num );
函数参数中的destsournum于memcpy函数的参数相同
memmove函数就是用来复制出现内存重叠的情况。
memmove函数的使用
int main()
{int arr[10] { 1,2,3,4,5,6,7,8,9,10 };memmove(arr, arr 2, 20);for (int i 0; i 10; i){printf(%d , arr[i]);}return 0;
}
这里就出现了内存重叠的情况将 3 4 5 6 7 放到1 2 3 4 5 的位置上
其中替换的部分 3 4 5 是的内存是重叠的memmove就是用来处理这种情况
memmove函数的模拟
memmove函数的模拟相对于memcpy函数的模拟来说相对麻烦一些分为一下三种情况下面我们来一个一个分析。 第一种情况 第二种情况 第三种情况 代码部分
void* my_memmove(void* dest, const void* sour, size_t num)
{void* ret dest;//记录目标位置的首地址//首先防止传进来的指针不为空assert(dest ! NULL);assert(sour ! NULL);//通过分析三种情况我们会发现如果dest指针小于sour指针就从前往后换//如果dest指针大于sour就需要从后往前换//如果dest的指针大于sournum从后往前或者从前往后都一样//判断dest和sourif (dest sour){while (num--){*(((char*)dest) num) *(((char*)sour) num);}}else if(dest sour || (char*)dest (char*)sour num){while (num--){*((char*)dest) *((char*)sour);(char*)dest ((char*)dest) 1;(char*)sour ((char*)sour) 1;}}return ret;//返回目标首地址
}
int main()
{int arr[10] { 1,2,3,4,5,6,7,8,9,10 };my_memmove(arr, arr 2, 20);for (int i 0; i 10; i){printf(%d , arr[i]);}return 0;
}
memset函数
是用来设置内存的将内存中的值以字节为单位设置成自己想要的内容。
void * memset ( void * ptr, int value, size_t num );
ptr要修改内存位置的首地址value要修改成的内容num要修改的字节数
memset函数的使用
int main()
{char prt[] Hello Word;memset(prt, x, 5);printf(%s, prt);//输出结果为xxxxx Wordreturn 0;
}
memcmp函数
memcmp函数用来比较从prt1和prt2指针指向的位置开始往后m个字节的内容进行比较如果prt1大于prt2就返回一个非零的值反之小于就返回小于零的值如果等于就返回零。
memcmp函数的使用
int main()
{char buf1[] asdfg;char buf2[] asdfi;int i memcmp(buf1, buf2, 5);printf(%d\n, i);if (i 0){printf(buf1buf2\n);}else if (i0){printf(buf1buf2\n);}else if (i0){printf(buf1buf2\n);}return 0;
}
这些就是我所学到的C语言重的内存函数请大佬们多多指错感谢 文章转载自: http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.qsy36.cn.gov.cn.qsy36.cn http://www.morning.tpfny.cn.gov.cn.tpfny.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn http://www.morning.clxpp.cn.gov.cn.clxpp.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.nxfuke.com.gov.cn.nxfuke.com http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.hongjp.com.gov.cn.hongjp.com http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.qxwrd.cn.gov.cn.qxwrd.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.mmqhq.cn.gov.cn.mmqhq.cn http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.jllnh.cn.gov.cn.jllnh.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn