Which of the following is used to allocate memory dynamically in C?

2026

Which of the following is used to allocate memory dynamically in C?

Answer: A. malloc()ConceptDynamic memory allocation lets a program request storage while it is running, when the required size or lifetime is not known at compile time. In C,…

  1. A.

    malloc()

  2. B.

    alloc()

  3. C.

    create()

  4. D.

    new()

Attempted by 196 students.

Show answer & explanation

Correct answer: A

Concept

Dynamic memory allocation lets a program request storage while it is running, when the required size or lifetime is not known at compile time.

In C, allocation functions are declared in the standard header <stdlib.h>, and allocated storage is accessed through a pointer.

Application

Consider reserving space for five integers at runtime.

  1. Compute the required size as 5 * sizeof(int).

  2. Call int *p = malloc(5 * sizeof *p);. The function requests one contiguous block of that many bytes and returns its starting address as a void * pointer, which C converts to int * on assignment.

  3. Check p != NULL before using the block, because allocation can fail.

  4. After use, call free(p); so the dynamically allocated block is released.

Cross-check and contrast

  • alloc() is not an ISO C standard-library allocation function.

  • create() is not an ISO C standard-library memory function; system APIs may use that name for other resources.

  • new is a C++ allocation operator rather than a C function named new().

  • malloc() is declared by ISO C for requesting a block of storage of a specified byte size.

Therefore, the required function is malloc().

Explore the full course: Tpsc Assistant Technical Officer

Loading lesson…