Interview Prep
Interview questions, roadmaps and practice sets
Tutorials
Free tutorials on 30+ topics
Blog
Career guides, skills and interview tips
Free PDFs
Notes, cheat sheets and question banks
Certificates
Free skill tests and course certificates
Tools & Whiteboard
Free tools you can use right in the browser
Practice Tests
Mock tests, MCQs and quick Q&A
Free LMS
Learn on the Uncodemy LMS
Choose the best training program according to your career.
Improve the performance of your business with our innovative Industrial Training Program.
Read More →
Our instructor-led training classes are designed to make you an expert.
Read More →
Real-life case studies are a great way to demonstrate your practical knowledge.
Read More →
Explore blogs, latest technologies and industry updates with practical examples.
Read More →Browse all available training programs and choose the right one for your career.
Read More →Know more about who we are and what we stand for.
Discover our mission to equip individuals with essential IT skills and industry expertise.
Read More →
Free tutorials, interview questions, tools and calculators to help you learn.
Explore →Learn, practice and plan your career — bilkul free.
Book a free video counselling session and pick the right course for you.
Read More →malloc() asks the C runtime for a block of memory on the heap while your program runs. This page shows its exact syntax, five small programs with their output, and the mistakes that lead to crashes and memory leaks.

The prototype is in <stdlib.h>:
void *malloc(size_t size);
sizeof so the size is right on every platform.void * to the first byte of the block, or NULL if the request cannot be met.The usual pattern is:
type *ptr = malloc(count * sizeof *ptr);
Writing sizeof *ptr instead of sizeof(int) means the size stays correct if you later change the pointer's type. In C you do not need to cast the result, because void * converts to any object pointer automatically. (C++ does need the cast, but C++ code normally uses new or containers instead.)
Heap memory lives outside the stack frame of the function that created it, so it survives after the function returns. See memory layout in C for where the heap sits next to the stack and data segment.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = malloc(sizeof *p);
if (p == NULL) {
fprintf(stderr, "Out of memory\n");
return 1;
}
*p = 42;
printf("Value: %d\n", *p);
free(p);
p = NULL;
return 0;
}
Output:
Value: 42
Setting p to NULL after free is a habit that turns an accidental second use into an obvious crash instead of silent corruption.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n = 5; /* could come from scanf or a file */
int *arr = malloc(n * sizeof *arr);
if (arr == NULL) return 1;
for (int i = 0; i < n; i++)
arr[i] = (i + 1) * (i + 1);
for (int i = 0; i < n; i++)
printf("%d%s", arr[i], i < n - 1 ? " " : "\n");
free(arr);
return 0;
}
Output:
1 4 9 16 25
Once allocated, the block is used exactly like a normal array: arr[i] is the same as *(arr + i). The pointer arithmetic guide explains why, and arrays in C covers fixed-size arrays for comparison.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char *src = "hello heap";
char *copy = malloc(strlen(src) + 1); /* +1 for the '\0' */
if (copy == NULL) return 1;
strcpy(copy, src);
printf("%s (%zu chars)\n", copy, strlen(copy));
free(copy);
return 0;
}
Output:
hello heap (10 chars)
Forgetting the + 1 for the terminating null byte is one of the most common heap overflows in C. More on how C stores text is in strings in C.
#include <stdio.h>
#include <stdlib.h>
struct Student {
int roll;
float marks;
};
int main(void) {
struct Student *s = malloc(sizeof *s);
if (s == NULL) return 1;
s->roll = 7;
s->marks = 88.5f;
printf("Roll %d scored %.1f\n", s->roll, s->marks);
free(s);
return 0;
}
Output:
Roll 7 scored 88.5
This is how nodes of a singly linked list are created: one malloc per node, linked through a pointer field.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t cap = 2, len = 0;
int *v = malloc(cap * sizeof *v);
if (v == NULL) return 1;
for (int x = 10; x <= 50; x += 10) {
if (len == cap) {
cap *= 2;
int *tmp = realloc(v, cap * sizeof *v);
if (tmp == NULL) { free(v); return 1; }
v = tmp;
}
v[len++] = x;
}
printf("len=%zu cap=%zu:", len, cap);
for (size_t i = 0; i < len; i++) printf(" %d", v[i]);
printf("\n");
free(v);
return 0;
}
Output:
len=5 cap=8: 10 20 30 40 50
Capacity grows 2 → 4 → 8. Assign the result of realloc to a temporary first: if it fails it returns NULL but leaves the old block alive, and writing v = realloc(v, ...) would lose your only pointer to it.
| Function | Signature | What it does |
|---|---|---|
| malloc | void *malloc(size_t size) | Allocates size bytes, contents indeterminate |
| calloc | void *calloc(size_t n, size_t size) | Allocates n * size bytes, all set to zero |
| realloc | void *realloc(void *ptr, size_t size) | Resizes a block, may move it; keeps old contents up to the smaller size |
| free | void free(void *ptr) | Releases a block; free(NULL) does nothing |
Use calloc when you need zeroed memory, such as counters. Common implementations of calloc also check n * size for overflow, which a hand-written malloc(n * size) does not.
malloc(n) for n ints gives n bytes, not n ints. sizeof(ptr) is the size of the pointer, not the data.free.NULL or a pointer you must not dereference but should still free.To catch these, compile with gcc -g -fsanitize=address prog.c (AddressSanitizer) or run the program under valgrind --leak-check=full ./a.out on Linux.
No. In C, void * converts to any object pointer type automatically, so int *p = malloc(n * sizeof *p); is correct. A cast is required only in C++.
It returns NULL. Always check the pointer before using it.
The block stays reserved until the program ends. In a long-running program, repeated leaks keep increasing memory use.
Reference: ISO/IEC 9899 (C standard), section 7.22.3 "Memory management functions"; cppreference.com pages for malloc, calloc, realloc and free.