malloc
Function Usage
Allocates memory.
Function Syntax
void *malloc(size_t size);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
size |
Number of bytes of the requested memory. |
The value cannot be negative. |
Input |
Return Value
- Success: A pointer to the allocated memory is returned.
- Failure: A null pointer is returned.
Example
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; } |
Perform the compilation.
1 | gcc run_your_application.c -o run_your_application -I/usr/local/ksl/include -L/usr/local/ksl/lib |
Run your application.
1 | LD_PRELOAD=/usr/local/ksl/lib/libkqmalloc.so ./run_your_application |
Output:
1 | malloc pointer address: ffff97ae8174 |
Parent topic: Function Syntax