calloc and realloc
Zeroed and resized memory.
calloc and realloc is a free C Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
Beyond malloc
Besides malloc, the standard library offers calloc for zeroed memory and realloc for resizing an existing block.
Both live in <stdlib.h>.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = calloc(3, sizeof(int));
printf("%d %d %d\n", p[0], p[1], p[2]);
free(p);
return 0;
}calloc Zeroes Memory
calloc(count, size) allocates room for count elements of size bytes and sets every byte to zero.
This is convenient when you need a clean slate.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = calloc(5, sizeof(int));
int sum = 0;
for (int i = 0; i < 5; i++) sum += a[i];
printf("sum of zeroed array = %d\n", sum);
free(a);
return 0;
}calloc Has Two Arguments
Unlike malloc, calloc takes the count and the element size separately.
It also guards against overflow when multiplying count by size.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
double *d = calloc(4, sizeof(double));
d[2] = 1.5;
printf("%f %f\n", d[0], d[2]);
free(d);
return 0;
}malloc vs calloc
Use malloc when you will overwrite every byte anyway, and calloc when you rely on the memory starting at zero.
calloc costs a little more because of the zeroing.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *m = malloc(2 * sizeof(int));
int *c = calloc(2, sizeof(int));
m[0] = 5; m[1] = 6;
printf("malloc: %d %d\n", m[0], m[1]);
printf("calloc: %d %d\n", c[0], c[1]);
free(m); free(c);
return 0;
}Growing with realloc
realloc(ptr, newsize) changes the size of a block, preserving its existing contents up to the smaller of the old and new sizes.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = malloc(2 * sizeof(int));
a[0] = 1; a[1] = 2;
a = realloc(a, 4 * sizeof(int));
a[2] = 3; a[3] = 4;
for (int i = 0; i < 4; i++) printf("%d ", a[i]);
printf("\n");
free(a);
return 0;
}realloc May Move the Block
realloc might return a different address if it relocates the block to find room.
Always assign the result back, and never keep using the old pointer.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = malloc(sizeof(int));
a[0] = 42;
int *bigger = realloc(a, 100 * sizeof(int));
printf("first element still %d\n", bigger[0]);
free(bigger);
return 0;
}Safe realloc Pattern
If realloc fails it returns NULL but leaves the original block intact.
Assign to a temporary first so you do not lose the old pointer on failure.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = malloc(2 * sizeof(int));
int *tmp = realloc(a, 8 * sizeof(int));
if (tmp == NULL) {
free(a);
return 1;
}
a = tmp;
a[7] = 99;
printf("%d\n", a[7]);
free(a);
return 0;
}Shrinking a Block
realloc can also make a block smaller. The leading elements are preserved; the rest is released.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = malloc(6 * sizeof(int));
for (int i = 0; i < 6; i++) a[i] = i;
a = realloc(a, 3 * sizeof(int));
printf("%d %d %d\n", a[0], a[1], a[2]);
free(a);
return 0;
}A Growing Buffer
A common pattern doubles the capacity when an array fills up, amortizing the cost of growth.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int cap = 2, len = 0;
int *a = malloc(cap * sizeof(int));
for (int v = 0; v < 5; v++) {
if (len == cap) {
cap *= 2;
a = realloc(a, cap * sizeof(int));
}
a[len++] = v;
}
printf("len=%d cap=%d last=%d\n", len, cap, a[len-1]);
free(a);
return 0;
}realloc with NULL
realloc(NULL, size) behaves exactly like malloc(size).
This lets a grow routine handle the very first allocation uniformly.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = realloc(NULL, 3 * sizeof(int));
a[0] = 7;
printf("%d\n", a[0]);
free(a);
return 0;
}Combining Them
You can start with calloc for a zeroed buffer and later grow it with realloc. New bytes after a grow are not zeroed.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = calloc(2, sizeof(int));
a = realloc(a, 4 * sizeof(int));
a[2] = 0; a[3] = 0;
printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
free(a);
return 0;
}Quick Check
Test your knowledge of calloc and realloc.
Recap
You learned two more allocation tools:
calloc(count, size)allocates zeroed memory and guards against multiply overflow.realloc(ptr, newsize)resizes a block, possibly moving it.- Always assign realloc's result to a temporary to avoid leaks on failure.
realloc(NULL, size)acts like malloc.
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “calloc and realloc” lesson free?
Yes — the full text of “calloc and realloc” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “calloc and realloc”?
Zeroed and resized memory. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “calloc and realloc” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- malloc and free
- calloc and realloc
- Memory Leaks
- Dangling Pointers