ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP1112Static And Dynamic Mem…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    3 min read
    Topic 18 of 19
    Topic Study Note Illustration
    18

    Static and Dynamic Memory Allocation

    Memory allocation in C++ can be broadly classified into two categories: static memory allocation and dynamic memory allocation . Understanding the differences between these two types of...

    Static and Dynamic Memory Allocation in C++

    Memory allocation in C++ can be broadly classified into two categories: static memory allocation and dynamic memory allocation. Understanding the differences between these two types of memory allocation is crucial for efficient memory management in your programs.

    1. Static Memory Allocation

    Static memory allocation occurs at compile time. The memory for variables is allocated when the program is compiled, and it remains allocated throughout the program's execution. This type of allocation is typically used for global variables, static variables, and local variables that are defined with fixed sizes.

    Key Features:

    • Fixed Size: The size of statically allocated memory must be known at compile time.
    • Lifetime: The memory remains allocated for the entire duration of the program.
    • Speed: Accessing statically allocated memory is generally faster because it is allocated on the stack or in the data segment.

    Examples:

    1. Global Variables:

      int globalVar = 10; // Allocated statically
      
    2. Static Local Variables:

      void func() {
          static int staticVar = 0; // Retains its value between function calls
          staticVar++;
          std::cout << staticVar << std::endl;
      }
      
    3. Array Declaration:

      int arr[10]; // Allocated statically with fixed size
      

    2. Dynamic Memory Allocation

    Dynamic memory allocation occurs at runtime. Memory is allocated on the heap as needed, and you can request and release memory during program execution. This flexibility is especially useful for managing variable-sized data structures, such as linked lists, trees, and arrays whose sizes are not known at compile time.

    Key Features:

    • Flexible Size: You can allocate memory of any size at runtime.
    • Lifetime: The memory remains allocated until it is explicitly released using delete or free().
    • Overhead: Dynamic allocation generally has a slight performance overhead due to the need for managing heap memory.

    Examples:

    1. Using new:

      int* ptr = new int; // Allocates memory for a single integer
      *ptr = 42; // Assign value to allocated memory
      delete ptr; // Free the allocated memory
      
    2. Allocating Arrays Dynamically:

      int size;
      std::cout << "Enter size: ";
      std::cin >> size;
      int* arr = new int[size]; // Allocates an array of integers
      // Use the array...
      delete[] arr; // Free the allocated array
      
    3. Using std::vector: The C++ Standard Library provides dynamic arrays through the std::vector class, which manages memory automatically.

      #include <vector>
      std::vector<int> vec; // Dynamic array
      vec.push_back(1); // Adding elements dynamically
      

    3. Memory Management

    • Static Allocation:

      • Managed automatically by the compiler.
      • No need to explicitly free memory.
    • Dynamic Allocation:

      • Requires explicit memory management.
      • Must use delete or delete[] to free memory after use.
      • Failure to do so can lead to memory leaks, where memory is no longer accessible but still allocated.

    Summary

    Understanding static and dynamic memory allocation is crucial for effective memory management in C++. Static memory allocation is fixed and efficient, while dynamic memory allocation offers flexibility and scalability at the cost of some performance overhead. Knowing when to use each type can help you write more efficient and robust programs.

    ← Previous topic 17
    Pointers/References
    Next topic 19 →
    File I/O Operations

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    518words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals