ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Static and Dynamic Memory Allocation
    Programming FundamentalsTopic 18 of 19

    Static and Dynamic Memory Allocation

    3 minread
    518words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count518
      Code examples0
      DifficultyBeginner