---
title: memset
description: "将目标内存中指定长度字节数的数据设置为指定的值。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_ksl_16_0025.html
sourcePath: /source/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_ksl_16_0025.html
indexId: 2ebcf57847a000e20068777d4d055e3ceb6ceb54c210224ee8b02a8f718b2dd276
---
# memset

#### 函数功能

将目标内存中指定长度字节数的数据设置为指定的值。


#### 函数定义

void *memset(void *destination, int value, size_t size);


#### 参数说明

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| destination | 指向用于存储重置内容的目标内存指针。 | 非空 | 输出 |
| value | 重置的目标值。 | 任意值 | 输入 |
| size | 被重置的字节数。 | 非负数 | 输入 |


#### 返回值

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


#### 示例

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

#define KQMALLOC_TEST_LEN 10

void MemsetExample()
{
    int8_t *destination = (int8_t *)malloc(KQMALLOC_TEST_LEN * sizeof(int8_t));
    if (destination == NULL) {
        printf("destination is null\n");
        return;
    } else {
        printf("destination address: %lx\n", destination);
        for (int8_t i = 0; i < KQMALLOC_TEST_LEN; ++i) {
            destination[i] = i;
        }
        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 *)memset(destination , 1, 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(destination);
}

int main(void) {
    MemsetExample();
    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
```

运行结果：

```
destination address: ffff9fe17174
destination test data before memcpy: 0 1 2 3 4 5 6 7 8 9
res address: ffff9fe17174
res test data after memcpy: 1 1 1 1 1 1 1 1 1 1
```
