📃
Julianne's Knowledge Base
  • README
  • Home
    • Computer Science and Programming
      • Data Structures
        • Array
        • Binary Heap
        • Binary Tree
        • Deque
        • Dynamic Array
        • Graph
        • Hash Table
        • Linked List
        • Queue
        • Stack
      • Databases
        • Database Normalization
      • Networking
        • IP Protocol
        • Network Devices
        • OSI Model and TCP/IP
        • Ethernet LAN Switching
        • IPv4 Addressing
        • Sockets
      • Operating Systems
        • UNIX Operating Systems
          • Fundamentals
            • Virtualization
              • CPU Virtualization
              • Processes
            • Processes
          • xv6
      • Software Development
        • General Tips
        • My Goals as a Software Developer
        • Programming Languages
          • C
            • Memory Management in C
          • C++
            • I/O in C++
            • Iterators
            • Memory Management in C++
          • Javascript/TypeScript
            • Inheritance
            • React
            • Useful Libraries
          • Python
        • Tools
          • GDB
          • Git
    • Cooking
      • Diet and Nutrition
      • Recipes
    • Languages
      • Japanese
        • Grammar
        • Numbers and Counting
    • Productivity
      • Getting Things Done (GTD)
      • My GTD Trigger List
      • My Personal Knowledge Management System
      • Obsidian
        • Plugins
        • Using Obsidian
Powered by GitBook
On this page
  • Pros
  • Cons
  • Implementation
  • Appending
  • Deleting
  • Links
  1. Home
  2. Computer Science and Programming
  3. Data Structures

Dynamic Array

A dynamic array is an array that has automatic resizing.

Pros

  • Fast lookups

  • Variable size

  • Cache-friendly (because items are stored in contiguous memory)

Cons

  • Worst-case for appends is slow (because underlying array needs to expand)

  • Inserts and deletes are costly (because items need to be shifted around)

Implementation

Dynamic arrays are usually implemented by encapsulating and manipulating a normal, underlying array. The size of the dynamic array is the number of items being held, and its capacity is the total number of items it can hold without needing to expand (which is equal to the length of the underlying array).

Appending

To add an item to a dynamic array when its underlying array is full, the underlying array will need to be expanded. In this case, the array cannot be expanded directly since other programs might be using that memory. Instead a new, larger array is created and the items are copied to the new array. The new array is usually double the size of the original.

Deleting

Removing items from dynamic arrays works just like removing from regular arrays, except items need to be shifted over to keep them contiguous in memory.

Links

PreviousDequeNextGraph

Last updated 5 years ago

Interview Cake - Dynamic Arrays