---
title: Malloc和Free
description: "- 申请指定字节的内存大小："
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0032.html
sourcePath: /source/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0032.html
indexId: 7eb5624e1d59351d081a074036f790b2829478b3cf867fafa63ff9174b9f34b277
---
# Malloc和Free

- 申请指定字节的内存大小：
  void* HMPP_Malloc(int32_t len);


- 释放内存：
  void HMPP_Free(void* ptr);


#### 参数

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| len | 字节长度（HMPP\_Malloc函数）。 数组长度（HMPPS\_Malloc\_xxx函数）。 | 大于0 | 输入 |
| ptr | 要释放内存的地址（Free函数）。 | 非空 | 输入 |
| width | HMPPI\_Malloc\_xxx\_kk（kk为C1/C2/C3/C4/AC4，表示通道的情况）将分配的连续内存看成一个width\*height的二维矩阵状态，width表示每行的元素个数。 | 大于0 | 输入 |
| height | HMPPI\_Malloc\_xxx\_kk中二维矩阵的行数。 | 大于0 | 输入 |
| step | HMPPI\_Malloc\_xxx\_kk中会将一行的字节数填充成64的整倍数，并将这个值存在step指定地址中返回。 | 非空 | 输出 |


#### 返回值

HMPP_Malloc函数：

- 成功：返回申请内存的首地址。
- 失败：返回NULL。


#### 错误码

无


#### 注意

Free函数入参一定是Malloc函数返回值。


#### 示例

```
#define BUFFER_SIZE 100
void Malloc_Free_Example() {
void *ptr = HMPP_Malloc(BUFFER_SIZE);
HMPP_Free(ptr);
uint8_t *ptrs = HMPPS_Malloc_8u(BUFFER_SIZE);
HMPPS_Free(ptrs);
int32_t step;
uint8_t *ptri = HMPPI_Malloc_8u_C1(BUFFER_SIZE, 4, &step);
printf("%d\n", step);
HMPPI_Free(ptri);
}
```

运行结果：

```
128
```
