Topic outline

  • Unit 4: Dynamic Memory Allocation

    We will now learn about dynamic memory allocation. Frequently, we know neither the size of the data nor the Data Structure when implementing programs and Data Structures. By learning about dynamic memory allocation, you will understand how to request memory during runtime. We will also discuss the risks of memory allocation and de-allocation, learning about memory leaks and dangling pointers, among other potential drawbacks. You will learn how to prevent these risks by utilizing the full capabilities of the C/C++ language to increase memory use efficiency.

    Completing this unit should take approximately 2 hours.

    • Upon successful completion of this unit, you will be able to:

      • construct and deconstruct memory-based CDTs.
        • formulate allocation and deallocation techniques for native C/C++ data types, structures, and classes.
      • choose appropriate means of making the best of use available memory in dynamic applications.
        • define the term memory leak.
        • define memory fragmentation.
        • determine approaches to eliminating memory leaks.
        • select methods for minimizing memory fragmentation.
    • 4.1: C++ Memory Allocation

      • Read these slides starting from Slide 13, "Scoping and Memory". These slides introduce how C++ is used to allocate and manage memory. Be sure you study memory allocation and management carefully, since it becomes increasingly important as data volume grows and as your program runs for longer periods of time.

      • Allocating memory is not sufficient; you also have to manage it. Pay special attention to the methods and terminology on this page. There are some interesting exercises at the end of this page that you should attempt to complete.

    • 4.2: C Memory Allocation

      • Memory allocation is not just for native data types; they work for user-defined types as well. Read this discussion, which uses C, but note that in C++ new and delete work as well.

      • You shouldn't feel limited by C++ new and delete. The C-language memory allocation methods are also effective.

    • 4.3: Memory Fragmentation

      • Memory fragmentation can result from dynamic memory allocation, or memory allocated at runtime instead of compile-time. Read the section on heaps to get a good definition.

      • It is possible to allocate and deallocate memory so that, even though there is sufficient memory overall, it has become so fragmented (chopped into pieces) that sufficient contiguous memory can no longer be found. At that point, no more memory allocations can take place. C/C++ does not have a native garbage-cleanup capability, so you either have to use a library to try to get it done or be careful in your allocations. This article gives advice on that topic; it is part of a larger discussion on optimization in C++.

    • 4.4: C/C++ Based on "Unsafe" Types

      • C/C++ allows memory allocated for one type of variable to be recast as another type of variable; which gives us the term "unsafe types". This kind of casting makes sense in a lot of cases but no sense in other cases. It can be very powerful but must be used with caution, as this article explains.

    • Unit 4 Assessment

      • Take this assessment to see how well you understood this unit.

        • This assessment does not count towards your grade. It is just for practice!
        • You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
        • You can take this assessment as many times as you want, whenever you want.