malloc
函数功能
内存分配。
函数定义
void *malloc(size_t size);
参数说明
参数名 |
描述 |
取值范围 |
输入/输出 |
---|---|---|---|
size |
申请内存的字节数。 |
非负数 |
输入 |
返回值
- 成功:返回指向分配内存的指针。
- 失败:返回空指针。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include "kqmalloc.h" #define KQMALLOC_TEST_LEN 10 void MallocExample() { int8_t *res = (int8_t *)malloc(KQMALLOC_TEST_LEN); if (res == NULL) { printf("res is null\n"); } else { printf("malloc pointer address: %lx\n", res); free(res); } } int main(void) { MallocExample(); return 0; } |
运行结果:
1 | malloc pointer address: ffff97ae8174 |
父主题: 函数定义