0
0
Cprogramming~20 mins

malloc function - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Malloc Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this malloc usage?
Consider the following C code snippet. What will be printed when it runs?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc(3 * sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    ptr[0] = 10;
    ptr[1] = 20;
    ptr[2] = 30;
    printf("%d %d %d\n", ptr[0], ptr[1], ptr[2]);
    free(ptr);
    return 0;
}
AGarbage values or segmentation fault
B0 0 0
CMemory allocation failed
D10 20 30
Attempts:
2 left
💡 Hint
Remember that malloc allocates memory but does not initialize it. Here, values are assigned before printing.
Predict Output
intermediate
2:00remaining
What error does this code raise?
What error will this C code produce when run?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = malloc(-5 * sizeof(int));
    if (ptr == NULL) {
        printf("Allocation failed\n");
        return 1;
    }
    ptr[0] = 1;
    printf("%d\n", ptr[0]);
    free(ptr);
    return 0;
}
APrints 1 without error
BCompilation error due to negative size
CRuntime error or undefined behavior due to negative size in malloc
DSegmentation fault immediately
Attempts:
2 left
💡 Hint
malloc expects a size_t argument, which is unsigned. Negative values convert to large positive numbers.
🔧 Debug
advanced
2:00remaining
Why does this malloc code cause a memory leak?
Examine the code below. Why does it cause a memory leak?
C
#include <stdlib.h>

int main() {
    int *ptr = malloc(10 * sizeof(int));
    ptr = malloc(20 * sizeof(int));
    free(ptr);
    return 0;
}
AThe first allocated memory is lost because ptr is overwritten before free, causing a leak
BNo memory leak occurs because free is called
Cfree(ptr) is called twice causing a leak
DThe second malloc fails and causes a leak
Attempts:
2 left
💡 Hint
Think about what happens to the pointer after the first malloc before the second malloc.
📝 Syntax
advanced
2:00remaining
Which option causes a compilation error?
Which of the following malloc usages will cause a compilation error?
Aint *p = malloc(5 * sizeof(int)); if (p == NULL) return 1;
Bint *p = malloc(5 * int);
Cint *p = malloc(5 * sizeof(int));
Dint *p = (int *)malloc(5 * sizeof(int));
Attempts:
2 left
💡 Hint
Check the syntax of the sizeof operator in each option.
🚀 Application
expert
2:00remaining
How many integers can be safely stored?
Given this code, how many integers can be safely stored in the allocated memory?
C
#include <stdlib.h>

int main() {
    int *arr = malloc(50 * sizeof(*arr));
    if (!arr) return 1;
    // How many integers can arr hold?
    free(arr);
    return 0;
}
A50 integers
Bsizeof(*arr) integers
C1 integer
DDepends on system architecture
Attempts:
2 left
💡 Hint
malloc allocates memory for 50 times the size of one integer.