Erik Fredericks, frederer@gvsu.edu Fall 2025
Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers
malloc(size)
void* malloc(size);
Example: int* p = (int*)malloc(4 * 100);
int* p = (int*)malloc(4 * 100);
Note, malloc (and those like it), require a new include:
malloc
#include <stdlib.h>
What if we don’t know the size of our type?
sizeof
long* p = (long*)malloc(sizeof(long) * 10);
calloc(count, size);
count * size
Example: float* p = calloc(64, sizeof(float));
float* p = calloc(64, sizeof(float));
Imagine we have an array of 16 ints.
int* p = (int*)malloc(sizeof(int) * 16);
p = realloc(p, sizeof(int) * 32);
Allocates new memory!
We need to free it when we’re done!
free(pointer);
If we forget, we can have memory leaks!
https://www.geeksforgeeks.org/c/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/
for malloc: 0 on system either because it is using the same block or security reasons - but not guaranteed and not in the standard!
degrades performance!