顯示包含「記憶體複製」標籤的文章。顯示所有文章
顯示包含「記憶體複製」標籤的文章。顯示所有文章

2022年6月10日星期五

C memmove

void    *memmove(void *dest, const void *src, size_t n);

n從一個記憶體位址複製n個資料到另一個位址,有點類似memcpy,但又有點不同

不同的地方在stack overflow有說明

memmove 可允許目標與來源位址相同

/*This file use to record string.h library use */
#include <string.h>
#include <stdio.h>
char SayBuf[10]="";//char buffer
char *hello="Hello\n";//show chars

int main()
{
    printf("Before memmove\n");
    printf("SayBuf:%s\n",SayBuf);
    //目標,來源,長度
    memmove(HelloBuf,HelloBuf,5);
    printf("After memmove\n");
    printf("SayBuf:%s\n",SayBuf);
    printf("HelloBuf:%s\n",HelloBuf);
    return 0;
}




可以看到在還沒有執行memmove之前,印出來的資料是空的,直到執行memmove之後才有資料

C memcpy

void    *memcpy(void *dest, const void *src, size_t n);

n從一個記憶體位址複製n個資料到另一個位址;


/*This file use to record string.h library use */
#include <string.h>
#include <stdio.h>
char SayBuf[10]="";//char buffer
char *hello="Hello\n";//show chars

int main()
{
    printf("Before memcpy\n");
    printf("SayBuf:%s\n",SayBuf);
    //目標,來源,長度
    memcpy(SayBuf,hello,5);
    printf("After memcpy\n");
    printf("SayBuf:%s\n",SayBuf);
    return 0;
}

可以看到在還沒有執行memcpy之前,印出來的資料是空的,直到執行memcpy之後才有資料

打賞按讚