C 퀴즈: malloc, calloc & free

This C quiz checks how you use malloc(), calloc(), realloc() and free() to work with memory on the heap. You will predict what small programs print and pick the safe way to allocate, grow and release a block.

보통 10문제 약 6분 합격 기준 80% C17

모드 선택

게스트로 플레이 중이에요. 로그인하면 XP와 연속 기록이 저장돼요.

예시 문제

10문제 중 1번

What does this print?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *a = malloc(5 * sizeof *a);
    if (a == NULL) return 1;
    for (int i = 0; i < 5; i++) a[i] = i * i;
    int sum = 0;
    for (int i = 0; i < 5; i++) sum += a[i];
    printf("%d\n", sum);
    free(a);
    return 0;
}
정답 보기

B. 30 The block holds the squares 0, 1, 4, 9 and 16, which add up to 30. sizeof *a is the size of one int, so 5 * sizeof *a is room for exactly five of them.

이 퀴즈에서 다루는 내용

  • Allocating arrays with malloc(n * sizeof *p)
  • calloc() vs malloc(): zeroed memory and the two arguments
  • Growing a block safely with realloc()
  • Checking for NULL and what free(NULL) does
  • Returning heap memory from a function

자주 묻는 질문

계정이 필요한가요?

아니요. 누구나 플레이할 수 있어요. 로그인하면 XP, 연속 기록, 배지, 기록이 저장돼요.

다시 풀 수 있나요?

네. 최고 점수가 저장되고, 문제와 보기는 매번 섞여서 나와요.