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,…
- A.
malloc()
- B.
alloc()
- C.
create()
- 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.
Compute the required size as
5 * sizeof(int).Call
int *p = malloc(5 * sizeof *p);. The function requests one contiguous block of that many bytes and returns its starting address as avoid *pointer, which C converts toint *on assignment.Check
p != NULLbefore using the block, because allocation can fail.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.newis a C++ allocation operator rather than a C function namednew().malloc()is declared by ISO C for requesting a block of storage of a specified byte size.
Therefore, the required function is malloc().