All Exams Test series for 1 year @ ₹349 only
Question

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

The correct answer is
A dynamically allocated array of integers created using malloc() function call

C Memory Management: Heap Allocation

Understanding where different types of data are stored in a C program's runtime environment is crucial. The main memory areas are the stack, the heap, and the static/global data segment.

Memory Regions Explained

  • Stack: Used for local variables, function parameters, and return addresses. Memory is automatically managed (allocated and deallocated) as functions are called and return.
  • Heap: A region of memory available for dynamic memory allocation. Memory must be explicitly requested (e.g., using $malloc()$, $calloc()$) and released (e.g., using $free()$).
  • Static/Global Data Segment: Used for global variables, static variables (both global and local), and string literals. Memory persists for the entire program duration.

Analyzing the Options

Let's examine each option based on C memory allocation:

  • Static Variable: A static variable declared inside a function is stored in the static/global data segment, not the heap. Its lifetime spans the entire program execution.
  • Local Array: An array of integers declared inside a function (without the $static$ keyword) is typically allocated on the stack. Its memory is managed automatically and is released when the function exits.
  • Dynamically Allocated Array: An array created using $malloc()$ is explicitly allocated from the heap. This memory persists until explicitly deallocated using $free()$.
  • Return Address: The return address, which tells the program where to resume execution after a function call, is stored on the stack.

Conclusion on Heap Storage

Based on the analysis, only the dynamically allocated array using $malloc()$ is stored in the heap segment of the C runtime environment. This is the area designated for memory allocation whose size or lifetime is not known at compile time.

Was this answer helpful?

Important Questions from Memory Management

  1. Which of the following is/are the basic units of IAS computer?

  2. Which of the following have the fastest access time?

  3. _________ is a memory management scheme that permits the physical address space of a process to be noncontiguous.

  4. Which of the following is a type of memory and is similar to RAM?

  5. Suppose you have eight 'black and white' images taken with a 1‐megapixel camera and one '8‐color' image taken by an 8‐megapixel camera. How much hard disk space in total do you need to store these images on your computer?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App