Skip to content

Latest commit

 

History

History
81 lines (58 loc) · 6.77 KB

2_Malloc() - Quick Revision.md

File metadata and controls

81 lines (58 loc) · 6.77 KB

Malloc() - Quick Revision 📘

  • malloc API: This is a standard C library function that is utilized for allocating memory chunks dynamically from the process's unused virtual address space.
  • Heap Memory Segment: The region of the virtual address space where dynamic memory allocation occurs is called the heap memory segment. Malloc takes memory from this segment when allocating memory.
  • realloc API: This is a variant of malloc and is used to resize memory that has already been allocated by the process.
  • Memory Allocation & Data Writing: The memory allocated by malloc is from the process's virtual address space, and when data is written to this space, it's mapped to a physical address in RAM through a process called paging. The physical memory (RAM) is where the data is actually stored.
  • MMU (Memory Management Unit): This is responsible for converting virtual addresses to physical addresses using paging. Actual data is written into physical memory, not virtual memory.
  • Virtual Memory and Physical Memory: Virtual memory consists of addresses, mapped to actual physical memory locations (RAM) where data is stored.

🤔 Curiosity

  1. Q1: How does malloc ensure memory allocation is in the unused virtual address space? A1: Malloc interacts with the operating system to ensure that the allocated memory comes from the process's unused virtual address space. It utilizes the heap memory manager of the OS to find a suitable block of unallocated memory and reserves it for the program.

  2. Q2: Can you explain the key differences between malloc() and realloc()? A2: Malloc is used to allocate a specific amount of memory for the first time, and returns a pointer to the first byte of the allocated space. Realloc, on the other hand, is used to resize previously allocated memory space (using malloc or calloc) and moves the data to a new location if needed, returning a pointer to the new location.

  3. Q3: What happens when malloc() is not able to allocate memory? A3: When malloc() cannot allocate memory, it returns a NULL pointer, and it is crucial for programs to check for this scenario to prevent dereferencing null pointers and ensure robust operation.

  4. Q4: Why do we need to manage memory in C using functions like malloc and free? A4: Memory management is crucial in C to efficiently utilize the available RAM. Functions like malloc and free allow programmers to allocate and deallocate memory dynamically, ensuring memory is used when needed and released when it's no longer required to avoid memory leaks and wastage.

  5. Q5: How does the operating system manage memory in relation to malloc and free? A5: The OS manages a process’s memory through the heap memory manager, which keeps track of allocated and free memory blocks. When malloc is called, the manager provides a block from the heap, and when free is called, it marks that block as available again, managing memory efficiently.

  6. Q6: Can malloc allocate memory continuously, and why does fragmentation occur? A6: Memory allocated by malloc is not guaranteed to be contiguous because it depends on the available blocks in the heap. Fragmentation occurs because, over time, memory allocation and deallocation create “holes” in the address space, which may be too small individually for larger memory requests, even if the total free space is sufficient.

3. 🌟 Concepts in Simple Words

  • malloc: Think of malloc like asking for a certain amount of boxes (bytes) where you can store your stuff (data). The place where these boxes are kept is called heap memory.
  • Heap Memory: It's like a big storage room in your computer where data is stored while your program is running. Malloc takes some space from this room.
  • realloc: Sometimes, you realize the boxes (memory) you asked for using malloc are not enough or maybe too many, so you use realloc to ask for a change (resize) without losing the stuff inside them.
  • Writing Data and Memory Types: Imagine writing a letter (data) and storing it in a locker (physical memory/RAM). Virtual memory is like having a list that tells which locker (physical address) corresponds to which friend (virtual address), so you always know where each letter is, but the letters are always in the lockers, not on your list.
  • MMU and Paging: This is like the postman who knows exactly where each locker (physical address) is and helps you find it using your list (virtual memory). So, you tell the postman the friend (virtual address) you have a letter for, and he knows exactly which locker to put it in.
  • Virtual and Physical Memory: In simple terms, physical memory is where your letters (data) are actually stored (like lockers or RAM), and virtual memory is like your organized list, helping you keep track of them without having to remember each locker’s location. So, your list (virtual memory) doesn’t hold the letters, but helps you (and the postman/MMU) know where they are in the storage room (physical memory).
Process's Perspective
(Virtual Address Space)
+----------------+------------+----------------+
| Used Memory    | Malloc(20) |  Free Memory   |
| (Previous use) | (0x0020)   |  (Unallocated) |
+----------------+------------+----------------+

            |
            |    MMU (Memory Management Unit)
            |    Maps Virtual Address to Physical Address
            V

MMU Mapping Table
+---------------------+
| Virt. Addr | Phys. Addr |
|------------|------------|
|   0x0020   |   0xA030   |
+---------------------+

            |
            |    Actual Data Writing in Physical Memory (RAM)
            V

Physical Memory (RAM)
+----------------+----------------+----------------+
| Used Memory    | Hello (Data)   |  Free Memory   |
| (Previous use) | (0xA030)       |  (Unallocated) |
+----------------+----------------+----------------+

Explanation:

  • Process's Perspective (Virtual Address Space): The process believes it's interacting with a contiguous block of memory, seeing a clear division between used memory, the newly allocated block (via malloc(20)), and the free memory. Address 0x0020 is assigned to the allocated block.

  • MMU Mapping Table: This is an abstraction of how the MMU keeps track of which virtual addresses correspond to which physical addresses. Here, virtual address 0x0020 is mapped to physical address 0xA030.

  • Physical Memory (RAM): The actual storage of data. The string "Hello" is stored in the memory location corresponding to physical address 0xA030, which is determined by the MMU.

Please note that addresses like 0x0020 and 0xA030 are just illustrative and actual memory addresses in a real system will be managed differently. The example serves to help understand the concept and the flow from virtual address through MMU to physical storage.