Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 1.05 KB

dynamic-memory.md

File metadata and controls

43 lines (33 loc) · 1.05 KB

Dynamic Memory Management

All memory allocation functions accept an argument of type size_t that specifies the number of bytes of memory to be allocated.

malloc()

The malloc function allocates space for an object of a specified size whose initial value is indeterminate. It All memory allocation functions accept an argument of type size_t that specifies the number of bytes of memory to be allocated.

Note: The returned value from it should be checked for error.

Here's an example of dynamically allocated memory for an integer:

int* p = malloc(sizeof(int));

Without a type

Memory can be allocated and stored in void pointer without specifying it's type.

void* p = malloc(size);

Before void type was introduced:

char* p = malloc(size);

Casting

It is often recommended to cast result of a memory allocation function call but isn't required.

int* p = (int*)malloc(sizeof(int));

calloc()

realloc()

free()

alloca()

aligned_alloc()

reallocarray()

Management issues