C 標準庫 – string.h之memmove使用

zhangrxiang發表於2019-05-10

memmove

  • Move block of memory
  • Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
  • The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
  • The function does not check for any terminating null character in source – it always copies exactly num bytes.
  • To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes.
  • 從 source 所指向的物件複製 num 個位元組到 destination 所指向的物件。兩個物件都被轉譯成 unsigned char 的陣列。物件可以重疊:如同複製字元到臨時陣列,再從該陣列到 destination 一般發生複製。
  • 從 source 複製 num 個字元到 destination,但是在重疊記憶體塊這方面,memmove() 是比 memcpy() 更安全的方法。如果目標區域和源區域有重疊的話,memmove() 能夠保證源串在被覆蓋之前將重疊區域的位元組拷貝到目標區域中,複製後源區域的內容會被更改。如果目標區域與源區域沒有重疊,則和 memcpy() 函式功能相同。
  • 若出現 destination 陣列末尾後的訪問則行為未定義。
  • 若 destination 或 source 為空指標則行為未定義。
void * memmove ( void * destination, const void * source, size_t num );

Parameters

destination

  • Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
  • 指向用於儲存複製內容的目標陣列,型別強制轉換為 void* 指標。
  • 指向複製目的物件的指標

source

  • Pointer to the source of data to be copied, type-casted to a pointer of type const void*.
  • 指向要複製的資料來源,型別強制轉換為 void* 指標。
  • 指向複製來源物件的指標

num

  • Number of bytes to copy.size_t is an unsigned integral type.
  • 要被複制的位元組數。
  • 要複製的位元組數

Return Value

  • destination is returned.
  • 該函式返回一個指向目標儲存區 destination 的指標。
  • 返回 destination 的副本,本質為更底層操作的臨時記憶體地址,在實際操作中不建議直接使用此地址,操作完成以後,真正有意義的地址是destination本身。

Example

//
// Created by zhangrongxiang on 2018/2/10.
//
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[32] = "I am your GOD";
    char str2[32] = "Hello World";
    char str3[] = "void * memmove ( void * destination, const void * source, size_t num );";
    memmove(str2, str, strlen(str));
    printf("%s
", str2);//I am your GOD
    memmove(str2, "hi hi", sizeof(str2));
    printf("%s
", str2);//hi
    printf("%c%c%c%c
", str2[3], str2[4], str2[5], str2[6]);//hi  
    memmove(str, str3, sizeof(str) - 1);
    for (int i = 0; i < sizeof(str); ++i) {
        printf("%c", str[i]);
    }//void * memmove ( void * destina%
/////////////////////////////////////////////////////////////////////
    char str4[] = "1234567890";
    puts(str4);//1234567890
    memmove(str4 + 4, str4 + 3, 3); // 從 [4,5,6] 複製到 [5,6,7]
    puts(str4);//1234456890
////////////////////////////////////////////////////////////////////
    // 設定分配的記憶體的有效型別為 int
    int *p = malloc(3 * sizeof(int));   // 分配的記憶體無有效型別
    int arr[3] = {1, 2, 3};
    memmove(p, arr, 3 * sizeof(int));     // 分配的記憶體現在擁有有效型別
    printf("%d%d%d
", p[0], p[1], p[2]);//123
    printf("%d%d%d
", *p, *(p + 1), *(p + 2));//123
    return 0;
}

文章參考

轉載註明出處

相關文章