柚子快報(bào)激活碼778899分享:開發(fā)語言 c語言內(nèi)存函數(shù)
一.memcpy函數(shù)
void * memcpy ( void * destination, const void * source, size_t num );
作用:函數(shù)memcpy從source的位置開始向后復(fù)制num個(gè)字節(jié)的數(shù)據(jù)到destination指向的內(nèi)存位置。
模擬實(shí)現(xiàn):
?
void* my_memcpy(void* destination, const void* source, size_t num)
{
char* ret = destination;
while (num--)
{
*(char*)destination = *(char*)source;
((char*)destination)++;
((char*)source)++;
}
return ret;
}
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]);
}
}
二.memmove函數(shù)
void * memmove ( void * destination, const void * source, size_t num );
作用:與memcpy類似,但和memcpy的差別就是memmove函數(shù)處理的源內(nèi)存塊和?標(biāo)內(nèi)存塊是可以重疊的
實(shí)現(xiàn):
void* my_memmove(void* destination, const void* source, size_t num)
{
char* ret = destination;
if (source >= destination || (char*)destination >= ((char*)source + num))
{
while (num)
{
*(char*)destination = *(char*)source;
((char*)destination)++;
((char*)source)++;
}
}
else
{
destination = (char*)destination + num - 1;
source = (char*)source + num - 1;
while (num--)
{
*(char*)destination = *(char*)source;
((char*)destination)--;
((char*)source)--;
}
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr1 + 4, arr1,20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
}
? ? 三.memset函數(shù)
void * memset ( void * ptr, int value, size_t num );
memset是?來設(shè)置內(nèi)存的,將內(nèi)存中的值以字節(jié)為單位設(shè)置成想要的內(nèi)容 ?
#include
#include
int main ()
{
char str[] = "hello world";
memset (str,'x',6);
printf(str);
return 0;
}
四.memcmp函數(shù)
int memcmp ( const void * ptr1, const void * ptr2, size_t num )
?較從ptr1和ptr2指針指向的位置開始,向后的num個(gè)字節(jié) ?
柚子快報(bào)激活碼778899分享:開發(fā)語言 c語言內(nèi)存函數(shù)
參考閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。