
前言
上篇文章學習了C語言字符串函數(shù),只是對字符串進行操作
本節(jié),小編整理了一下C語言中的內(nèi)存函數(shù),對內(nèi)存進行操作,只針對會內(nèi)存塊,不針對數(shù)據(jù)
memcpy
概述
void * memcpy ( void * destination, const void * source, size_t num );
memcpy是對內(nèi)存拷貝
拷貝的可能是字符串,也可能是整型數(shù)組
所以使用 void*
將source拷貝到destination,指定字節(jié)數(shù)為num
code
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
memcpy(arr2, arr1, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
arr2是dest,arr1是scr,20是怒num,即字節(jié)
將arr1中的前20個字節(jié)(即1,2,3,4,5)復(fù)制到arr2中
運行結(jié)果
1 2 3 4 5 0 0 0 0 0
模擬實現(xiàn)
#include <stdio.h>
#include <string.h>
#include<assert.h>
void* my_memcpy(void* dest,const void* src, size_t num)
{
void* ret = dest; //后面dest的地址可能會發(fā)生改變,因此先將dest的首地址存放在ret中
assert(dest && src);
while (num--)
{
*(char*) dest = *(char*)src; //viod*型不能解引用,所以先強制類型轉(zhuǎn)換成char*型,再解引用
dest = (char*)dest+1;
src = (char*)src+1;
}
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
my_memcpy(arr2, arr1, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
對于重疊的內(nèi)存,交給memmove來處理
其實在vs中,memcpy庫函數(shù)也可以處理重疊內(nèi)存,但是在其他編譯器不一定可以,不通用
memmove
概述
void * memmove ( void * destination, const void * source, size_t num );
? 和memcpy的差別就是memmove函數(shù)處理的源內(nèi)存塊和?標內(nèi)存塊是可以重疊的。
? 如果源空間和?標空間出現(xiàn)重疊,就得使?memmove函數(shù)處理。
將1 2 3 4 5拷貝到3 4 5 6 7的位置上
即,最終結(jié)果為,1 2 1 2 3 4 5 8 9 10
code
#include <stdio.h>
#include <string.h>
#include<assert.h>
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
memmove(arr1 + 2, arr1, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
輸出結(jié)果
1 2 1 2 3 4 5 8 9 10
模擬實現(xiàn)
前面我們看到,
將1 2 3 4 5拷貝到3 4 5 6 7的位置上
實際上應(yīng)該是
將5放到7的位置
將4放到6的位置
將3放到5的位置
將4放到4的位置
將3放到3的位置
是后—>前
如果,先將1放到3的位置,那么3這個位置就變成1。再將2放到4的位置,此時最終結(jié)果就變成了1 2 1 2 1 2 1 8 9 10
再來看看這種情況:
將5 6 7 8 9拷貝到3 4 5 6 7的位置上
將5放到3的位置上
將6放到4的位置上
(此時原本5 6位置上就沒有數(shù)字了)
將7放到5的位置
將8放到6的位置
將9放到7的位置
是前—>后
因此,
if(dest<src):前—>后
else:后—>前
模擬code
#include <stdio.h>
#include <string.h>
#include<assert.h>
void* my_memmove(void* dest, const void* src, size_t num)
{
void* ret = dest;
assert(dest);
assert(src);
//分情況討論
if (dest < src)
{
//前->后
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
//后->前
while (num--)
{
//num=18
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr1, arr1+2, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
輸出結(jié)果
3 4 5 6 7 6 7 8 9 10
memset
memory set 記憶設(shè)置,即內(nèi)存設(shè)置
void * memset ( void * ptr, int value, size_t num );
memset是?來設(shè)置內(nèi)存的,將內(nèi)存中的值以字節(jié)為單位設(shè)置成想要的內(nèi)容。
code
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "hello world";
memset(str, 'x', 6);
printf(str);
return 0;
}
注意點:memset是以字節(jié)為單位設(shè)置內(nèi)存值的
輸出結(jié)果
xxxxxxworld
memcmp
和strcmp功能其實差不多
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
?較從ptr1和ptr2指針指向的位置開始,向后的num個字節(jié)
code
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abqwertyuiop";
int ret = memcmp(arr1, arr2, 2);
printf("%d\n", ret);
return 0;
}
輸出結(jié)果
0
總結(jié)
小編看來,內(nèi)存函數(shù)和字符串函數(shù)有很多相似的地方,只不過內(nèi)存函數(shù)針對的內(nèi)存塊罷了。
下一篇更新:數(shù)據(jù)在內(nèi)存中的儲存文章來源:http://www.zghlxwxcb.cn/news/detail-708157.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-708157.html
到了這里,關(guān)于C語言學習系列-->一篇帶你看懂內(nèi)存函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!