Question:

In a C runtime environment, which one of the following is stored in the heap?

Show Hint

Only memory obtained through malloc() (or calloc/realloc) lives on the heap and must be freed explicitly; locals, return addresses go on the stack and statics live in the data segment.
Updated On: Jul 22, 2026
  • A static variable declared inside a function
  • An array of integers declared inside a function
  • A dynamically allocated array of integers created using a malloc() function call
  • Return address of a function
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Step 1: Recall the memory regions of a typical C runtime environment.
A running C program's memory is divided into several regions. The stack holds local (automatic) variables and function-call information such as return addresses, and grows and shrinks automatically as functions are called and return. The static/data segment holds variables that exist for the entire lifetime of the program, such as global variables and variables declared with the static keyword. The heap holds memory that the programmer explicitly requests at run time using functions like malloc(), calloc(), or realloc(), and this memory persists until it is explicitly freed with free(), not tied to any single function call.

Step 2: Check option (A), a static variable declared inside a function.
Even though it is declared inside a function, the static keyword makes this variable retain its value across calls and gives it a fixed memory location for the whole program's lifetime, exactly like a global variable, so it is stored in the static/data segment, not the heap.

Step 3: Check option (B), an array of integers declared inside a function.
A plain array declared inside a function without static or dynamic allocation is a local (automatic) variable. Its size is fixed at compile time, and its memory is automatically allocated when the function is entered and automatically released when the function returns, exactly the behavior of the stack, not the heap.

Step 4: Check option (C), a dynamically allocated array of integers created using malloc().
The whole purpose of malloc() is to request memory from the heap at run time. This memory is not tied to the function that called malloc(), it survives even after that function returns, and it remains allocated until the program explicitly calls free() on it (or the program ends). This is the defining characteristic of heap memory, so option (C) is exactly the case that is stored in the heap.

Step 5: Check option (D), the return address of a function.
When a function is called, the address to resume execution at (the return address) is pushed onto the call stack as part of the function's stack frame, alongside other bookkeeping information, and is popped off when the function returns. This is a core stack operation, not heap memory.

Step 6: Conclusion.
Static variables and the return address live in fixed program-lifetime storage and the stack respectively, and a plain local array lives on the stack, but memory obtained through malloc() is, by definition, heap memory.
\[ \boxed{\text{Option C: dynamically allocated array via malloc()}} \]
Was this answer helpful?
0
0