C examples for Memory:malloc
This dynamically allocated memory is uninitialized and can only be accessed through pointers.
#include <stdio.h> #include <stdlib.h> int main(void) { /* Dynamic memory allocation */ char* ptr = (char*)malloc(sizeof(char) * 5); }
The following code uses if statement to check if it has failed to allocate the memory.
char* ptr = malloc(sizeof(char) * 5); if (ptr == NULL) { /* No memory allocated, exit function */ return -1; }