---
title: memcpy
description: "从源内存中拷贝指定长度字节数的数据至目标内存中。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/252RC1/accel/kunpengaccel_ksl_16_0024.html
sourcePath: /source/zh/kunpengboostkithistory/252RC1/accel/kunpengaccel_ksl_16_0024.html
indexId: baaeb10a2973dccfc8f49850ca83f9d5907bff24d2d9ef33d922a6ba0aec754076
---
# memcpy

#### 函数功能

从源内存中拷贝指定长度字节数的数据至目标内存中。


#### 函数定义

void *memcpy(void * destination, const void *source, size_t size);


#### 参数说明

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| destination | 指向用于存储复制内容的目标内存指针。 | 非空 | 输出 |
| source | 指向要复制的数据源内存指针。 | 非空 | 输入 |
| size | 被复制的字节数。 | 非负数 | 输入 |


#### 返回值

- 成功：返回指向目标存储区内存的指针。
- 失败：返回空指针。


#### 示例

```
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "kqmalloc.h"

#define KQMALLOC_TEST_LEN 10

void MemcpyExample()
{
    int8_t *source = (int8_t *)malloc(KQMALLOC_TEST_LEN * sizeof(int8_t));
    if (source == NULL) {
        printf("source is null\n");
        return;
    } else {
        printf("source address: %lx\n", source);
        for (int8_t i = 0; i < KQMALLOC_TEST_LEN; ++i) {
            source[i] = i;
        }
        printf("source test data:");
        for (int8_t i = 0; i < KQMALLOC_TEST_LEN; ++i) {
            printf(" %d", source[i]);
        }
        printf("\n");
    }

    int8_t *destination = (int8_t *)malloc(KQMALLOC_TEST_LEN * sizeof(int8_t));
    if (destination == NULL) {
        printf("destination is null\n");
        free(source);
        return;
    } else {
        printf("destination address: %lx\n", destination);
        printf("destination test data before memcpy:");
        for (int8_t i = 0; i < KQMALLOC_TEST_LEN; ++i) {
            printf(" %d", destination[i]);
        }
        printf("\n");
    }
    int8_t *res = (int8_t *)memcpy(destination , source, KQMALLOC_TEST_LEN * sizeof(int8_t));
    printf("res address: %lx\n", res);
    if (res != NULL) {
        printf("res test data after memcpy:");
        for (int8_t i = 0; i < KQMALLOC_TEST_LEN; ++i) {
            printf(" %d", res[i]);
        }
        printf("\n");
    }

    free(source);
    free(destination);
}

int main(void) {
    MemcpyExample();
    return 0;
}
```

编译。

```
gcc run_your_application.c -o run_your_application -I/usr/local/ksl/include -L/usr/local/ksl/lib
```

运行。

```
LD_PRELOAD=/usr/local/ksl/lib/libkqmalloc.so ./run_your_application
```

运行结果：

```
source address: ffffbb202174
source test data: 0 1 2 3 4 5 6 7 8 9
destination address: ffffbb202168
destination test data before memcpy: 92 33 32 -69 -1 -1 0 0 0 0
res address: ffffbb202168
res test data after memcpy: 0 1 2 3 4 5 6 7 8 9
```
