Question:easy

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

Show Hint

Match each item to its memory region: static variables and globals go to the data segment, ordinary local variables and return addresses go to the stack, and anything obtained via malloc/calloc/realloc goes to the heap.
Updated On: Aug 3, 2026
  • A static variable declared inside a function
  • An array of integers declared inside a function
  • A dynamically allocated array of integers created using malloc() function call
  • Return address of a function
Show Solution

The Correct Option is C

Solution and Explanation

Think of the four memory regions a running C program uses as four separate shelves: code, data/BSS, stack, and heap.

A static local variable sits on the data/BSS shelf because the compiler reserves it a fixed slot that exists for the whole run of the program, regardless of function calls. A local array declared without malloc sits on the stack shelf, since it is created when the function is entered and destroyed when the function returns, just like any other local variable. The return address that tells the CPU where to resume after a function call also lives on the stack shelf, packed into the function's stack frame alongside local variables and saved registers.

The only shelf left is the heap, and that is precisely where memory obtained through \(malloc()\) lands. Calling \(malloc()\) asks the runtime system for a block of memory that will remain valid until it is explicitly freed with \(free()\), independent of which function is currently executing. This dynamic, programmer-controlled lifetime is the defining feature of heap memory.

So among the four choices, only the malloc-allocated integer array belongs to the heap.

Final answer: Option (C).
Was this answer helpful?
0


Questions Asked in GATE CS exam