Skip to content
LAM
Read Home Blog
Make Projects HTML Tools Games
Touch grass Notes Resume Links
Home Blog HTML Projects
Tools Games Notes Resume Links
Back CSE 240 Module 6 - Memory Management & Object-Oriented Programming Programming
Download Open
Show description 2,055 chars · Programming

CSE 240 Module 6 - Memory Management & Object-Oriented Programming

CSE 240 Module 6 - Memory Management & Object-Oriented Programming




CSE 240 Module 6

Memory Management & Object-Oriented Programming Cheat Sheet





Table of Contents


1. Memory Management & Garbage Collection

2. Constructors & Destructors

3. Memory Types (Stack, Heap, Static)

4. Object-Oriented Programming Concepts

5. Class Definition & Implementation

6. Access Control & Information Hiding

7. Practical Examples






1. Memory Management & Garbage Collection



Garbage Collection in C/C++


Critical Rules:


C/C++ programmers must correctly decide when to free unused memory created with malloc()/new

Memory leakage happens if programmer forgets to free()/delete unused memory

For each malloc()/new, one must use corresponding free()/delete

Number of free()/delete used should equal number of malloc()/new used








Garbage Collection Comparison




Language

Garbage Collection

Advantages

Disadvantages






Java

Automatic (reference counter)

No manual memory management needed

May not run often enough, bad for real-time systems




C++

Manual (programmer controlled)

Complete control over timing

Memory leaks, dangling references possible









Binary Tree Deletion Example

void dTree(TreeNode *p) {
if (p == 0) return;
if (p->left != 0) // size-m problem
dTree(p->left);
if (p->right != 0) // size-m problem
dTree(p->right);
delete p; // What traversing order is used?
return;
}

Answer: This uses postorder traversal - deletes children first, then parent.





Deleting Collection of Objects


Best Practices:

Linked List: Use loop with temporal pointer, delete forwards or backwards

Tree: Use pre-order, in-order, or post-order traversing to delete each node

Array/Array of Arrays: Delete array, then each pointer, then each structure





// Deleting Linked List Step by Step (While-Loop)
temp = head;
while (temp != NULL) {
temp = temp->next;
delete head; // C uses free(head);
head = temp;
}







2.…

CSE 240 Module 6 - Memory Management & Object-Oriented Programming

31,698 bytes · HTML source
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSE 240 Module 6 - Memory Management & Object-Oriented Programming</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            background-color: #ffffff;
            color: #333;
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background: linear-gradient(135deg, #e67e22, #f39c12);
            color: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        .header p {
            font-size: 1.2em;
            opacity: 0.9;
        }

        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 2px solid #f39c12;
            border-radius: 10px;
            background-color: #fef9e7;
        }

        .section-title {
            font-size: 1.8em;
            color: #d35400;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 3px solid #f39c12;
        }

        .subsection {
            margin-bottom: 20px;
            padding: 15px;
            background-color: white;
            border-left: 4px solid #f39c12;
            border-radius: 5px;
        }

        .subsection h3 {
            color: #e67e22;
            margin-bottom: 10px;
            font-size: 1.3em;
        }

        .code-block {
            background-color: #f4f4f4;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            margin: 10px 0;
            font-family: 'Courier New', monospace;
            overflow-x: auto;
            border-left: 4px solid #f39c12;
        }

        .formula {
            background-color: #fff3cd;
            border: 2px solid #f39c12;
            border-radius: 5px;
            padding: 10px;
            margin: 10px 0;
            text-align: center;
            font-weight: bold;
            color: #d35400;
        }

        .step-box {
            background-color: #fff3cd;
            border: 2px solid #f39c12;
            border-radius: 8px;
            padding: 15px;
            margin: 10px 0;
        }

        .step-number {
            display: inline-block;
            background-color: #e67e22;
            color: white;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            margin-right: 10px;
            font-weight: bold;
        }

        .comparison-table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
            background-color: white;
        }

        .comparison-table th, .comparison-table td {
            border: 1px solid #f39c12;
            padding: 12px;
            text-align: left;
        }

        .comparison-table th {
            background-color: #e67e22;
            color: white;
            font-weight: bold;
        }

        .comparison-table tr:nth-child(even) {
            background-color: #fef9e7;
        }

        .highlight {
            background-color: #fff3cd;
            padding: 2px 4px;
            border-radius: 3px;
            font-weight: bold;
            color: #d35400;
        }

        .memory-diagram {
            text-align: center;
            font-family: monospace;
            background-color: #f9f9f9;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin: 10px 0;
        }

        .button {
            display: inline-block;
            padding: 10px 20px;
            background-color: #e67e22;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            margin: 5px;
            border: none;
            cursor: pointer;
            font-size: 14px;
        }

        .button:hover {
            background-color: #d35400;
        }

        .two-column {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin: 15px 0;
        }

        @media (max-width: 768px) {
            .two-column {
                grid-template-columns: 1fr;
            }
        }

        .key-points {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            border-left: 4px solid #f39c12;
            padding: 15px;
            margin: 10px 0;
            border-radius: 5px;
        }

        .toc {
            background-color: #fef9e7;
            border: 2px solid #f39c12;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 30px;
        }

        .toc h2 {
            color: #d35400;
            margin-bottom: 15px;
        }

        .toc ul {
            list-style-type: none;
        }

        .toc li {
            margin: 8px 0;
        }

        .toc a {
            color: #e67e22;
            text-decoration: none;
            font-weight: 500;
        }

        .toc a:hover {
            text-decoration: underline;
        }

        .warning-box {
            background-color: #fcf3cf;
            border: 2px solid #f7dc6f;
            border-left: 4px solid #f39c12;
            padding: 15px;
            margin: 10px 0;
            border-radius: 5px;
        }

        .warning-box h4 {
            color: #d35400;
            margin-bottom: 8px;
        }

        .access-level {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.8em;
            font-weight: bold;
            margin-right: 5px;
        }

        .public {
            background-color: #d4edda;
            color: #155724;
        }

        .private {
            background-color: #f8d7da;
            color: #721c24;
        }

        .protected {
            background-color: #fff3cd;
            color: #856404;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>CSE 240 Module 6</h1>
        <p>Memory Management & Object-Oriented Programming Cheat Sheet</p>
    </div>

    <div class="toc">
        <h2>Table of Contents</h2>
        <ul>
            <li><a href="#memory-management">1. Memory Management & Garbage Collection</a></li>
            <li><a href="#constructors-destructors">2. Constructors & Destructors</a></li>
            <li><a href="#memory-types">3. Memory Types (Stack, Heap, Static)</a></li>
            <li><a href="#oop-concepts">4. Object-Oriented Programming Concepts</a></li>
            <li><a href="#class-implementation">5. Class Definition & Implementation</a></li>
            <li><a href="#access-control">6. Access Control & Information Hiding</a></li>
            <li><a href="#practical-examples">7. Practical Examples</a></li>
        </ul>
    </div>

    <div id="memory-management" class="section">
        <h2 class="section-title">1. Memory Management & Garbage Collection</h2>
        
        <div class="subsection">
            <h3>Garbage Collection in C/C++</h3>
            <div class="warning-box">
                <h4>Critical Rules:</h4>
                <ul>
                    <li>C/C++ programmers must correctly decide when to free unused memory created with <code>malloc()/new</code></li>
                    <li><strong>Memory leakage</strong> happens if programmer forgets to <code>free()/delete</code> unused memory</li>
                    <li>For each <code>malloc()/new</code>, one must use corresponding <code>free()/delete</code></li>
                    <li>Number of <code>free()/delete</code> used should equal number of <code>malloc()/new</code> used</li>
                </ul>
            </div>
        </div>

        <div class="subsection">
            <h3>Garbage Collection Comparison</h3>
            <table class="comparison-table">
                <thead>
                    <tr>
                        <th>Language</th>
                        <th>Garbage Collection</th>
                        <th>Advantages</th>
                        <th>Disadvantages</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><strong>Java</strong></td>
                        <td>Automatic (reference counter)</td>
                        <td>No manual memory management needed</td>
                        <td>May not run often enough, bad for real-time systems</td>
                    </tr>
                    <tr>
                        <td><strong>C++</strong></td>
                        <td>Manual (programmer controlled)</td>
                        <td>Complete control over timing</td>
                        <td>Memory leaks, dangling references possible</td>
                    </tr>
                </tbody>
            </table>
        </div>

        <div class="subsection">
            <h3>Binary Tree Deletion Example</h3>
            <div class="code-block">void dTree(TreeNode *p) {
    if (p == 0) return;
    if (p->left != 0)        // size-m problem
        dTree(p->left);
    if (p->right != 0)       // size-m problem  
        dTree(p->right);
    delete p;                // What traversing order is used?
    return;
}</div>
            <p><strong>Answer:</strong> This uses <span class="highlight">postorder traversal</span> - deletes children first, then parent.</p>
        </div>

        <div class="subsection">
            <h3>Deleting Collection of Objects</h3>
            <div class="key-points">
                <strong>Best Practices:</strong>
                <ul>
                    <li><strong>Linked List:</strong> Use loop with temporal pointer, delete forwards or backwards</li>
                    <li><strong>Tree:</strong> Use pre-order, in-order, or post-order traversing to delete each node</li>
                    <li><strong>Array/Array of Arrays:</strong> Delete array, then each pointer, then each structure</li>
                </ul>
            </div>

            <div class="code-block">// Deleting Linked List Step by Step (While-Loop)
temp = head;
while (temp != NULL) {
    temp = temp->next;
    delete head;        // C uses free(head);
    head = temp;
}</div>
        </div>
    </div>

    <div id="constructors-destructors" class="section">
        <h2 class="section-title">2. Constructors & Destructors</h2>

        <div class="subsection">
            <h3>Constructor Basics</h3>
            <div class="key-points">
                <strong>Constructor Properties:</strong>
                <ul>
                    <li>Function whose name is same as class name</li>
                    <li>Used to automatically initialize objects</li>
                    <li>Special kind of member function</li>
                    <li>Automatically called when object declared</li>
                    <li>Must have same name as class</li>
                    <li>Cannot return a value; not even void</li>
                    <li>Must be in public section</li>
                    <li>Can be overloaded with different parameters</li>
                </ul>
            </div>
        </div>

        <div class="subsection">
            <h3>Constructor Example</h3>
            <div class="code-block">class Queue {
private:
    int queue_size;
protected:
    int *buffer;
    int front;
    int rear;
public:
    Queue(void) {           // Default constructor
        front = 0; rear = 0;
        queue_size = 0;
        buffer = NULL;
    }
    
    Queue(int n) {          // Constructor overload
        front = 0; rear = 0;
        queue_size = n;
        buffer = new int[queue_size];
    }
    
    virtual ~Queue(void) {  // Destructor (~ tilde)
        delete buffer;
        buffer = NULL;
    }
};</div>
        </div>

        <div class="subsection">
            <h3>Destructor Basics</h3>
            <div class="key-points">
                <strong>Destructor Properties:</strong>
                <ul>
                    <li>Opposite of constructor</li>
                    <li>Automatically called when object is out-of-scope</li>
                    <li>Default version only removes ordinary variables, not dynamic variables</li>
                    <li>Dynamically-allocated variables don't go away until "deleted"</li>
                    <li>Destructors help to "deallocate" them when object destroyed</li>
                    <li>Defined like constructor, just add ~ (tilde)</li>
                </ul>
            </div>
        </div>

        <div class="subsection">
            <h3>Responsibilities of Garbage Collection</h3>
            <div class="warning-box">
                <h4>Class Writer vs Class User:</h4>
                <ul>
                    <li><strong>Class Writer:</strong> If heap memory (<code>new()</code>) used in constructor, destructor must delete the memory</li>
                    <li><strong>Class Users:</strong> NOT responsible for garbage collection of memory they didn't explicitly create</li>
                    <li><strong>Rule:</strong> Number of <code>delete</code>s must equal number of <code>new()</code>s</li>
                </ul>
            </div>
        </div>
    </div>

    <div id="memory-types" class="section">
        <h2 class="section-title">3. Memory Types (Stack, Heap, Static)</h2>

        <div class="subsection">
            <h3>Program Memory Partition</h3>
            <div class="memory-diagram">
                <strong>OS Memory Allocation</strong><br><br>
                ┌─────────────────────┐ ← Starting Address<br>
                │   Program Code      │ (size known at compilation)<br>
                ├─────────────────────┤<br>
                │ Global & Static     │ (size known at compilation)<br>
                │ Variables & Objects │<br>
                ├─────────────────────┤ ← Heap Pointer<br>
                │       Heap          │ (Variables created with malloc/new)<br>
                │        ↓            │<br>
                │        ↑            │<br>
                │       Stack         │ (Local variables & functions)<br>
                └─────────────────────┘ ← Stack Pointer
            </div>
        </div>

        <div class="subsection">
            <h3>Memory Management Rules</h3>
            <table class="comparison-table">
                <thead>
                    <tr>
                        <th>Memory Type</th>
                        <th>Allocation</th>
                        <th>Variables</th>
                        <th>Lifetime</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><strong>Static Memory</strong></td>
                        <td>Compilation time</td>
                        <td>Global variables, static local variables</td>
                        <td>Entire program execution</td>
                    </tr>
                    <tr>
                        <td><strong>Stack</strong></td>
                        <td>Function calls</td>
                        <td>Non-static local variables</td>
                        <td>Function scope</td>
                    </tr>
                    <tr>
                        <td><strong>Heap</strong></td>
                        <td>Dynamic (malloc/new)</td>
                        <td>Dynamically allocated variables</td>
                        <td>Until free/delete called</td>
                    </tr>
                </tbody>
            </table>
        </div>

        <div class="subsection">
            <h3>Stack Memory for Function Calls</h3>
            <div class="key-points">
                <strong>Stack Frame Components:</strong>
                <ul>
                    <li><strong>sp:</strong> stack pointer</li>
                    <li><strong>fp:</strong> frame pointer (access local variables)</li>
                    <li><strong>Return address:</strong> saved for function return</li>
                    <li><strong>Local variables:</strong> stored in frame</li>
                </ul>
            </div>

            <div class="memory-diagram">
                Before Call → Save Return Address → Make Frame<br>
                ┌──────────┐   ┌──────────────┐   ┌─────────────┐<br>
                │ occupied │   │ return addr  │   │ stack frame │<br>
                └──────────┘   │ occupied     │   │ return addr │<br>
                               └──────────────┘   │ occupied    │<br>
                                                  └─────────────┘
            </div>
        </div>

        <div class="subsection">
            <h3>Static Memory Usage</h3>
            <div class="code-block">void login() {
    static int counter = 0;     // initialized only once
    if (verified()) counter++;  // count # of users logged in
}</div>

            <div class="key-points">
                <strong>Static Local vs Global Variables:</strong>
                <ul>
                    <li><strong>Readability:</strong> Static local puts declaration where variable used</li>
                    <li><strong>Security:</strong> Prevents other functions from accessing the variable</li>
                    <li><strong>Alternative:</strong> Put login/logout in one class with static class variable</li>
                </ul>
            </div>
        </div>
    </div>

    <div id="oop-concepts" class="section">
        <h2 class="section-title">4. Object-Oriented Programming Concepts</h2>

        <div class="subsection">
            <h3>Core OOP Principles</h3>
            <div class="two-column">
                <div>
                    <h4>Information Hiding</h4>
                    <p>Details of how operations work not known to "user" of class</p>
                </div>
                <div>
                    <h4>Data Abstraction</h4>
                    <p>Details of how data is manipulated within ADT/class not known to user</p>
                </div>
            </div>
            <div class="key-points">
                <strong>Encapsulation:</strong> Bring together data and operations, but keep "details" hidden
            </div>
        </div>

        <div class="subsection">
            <h3>Object-Oriented Paradigm Features</h3>
            <ul>
                <li><strong>Abstract data type (class):</strong> Encapsulation of state accessible only through defined operations</li>
                <li><strong>Inheritance:</strong> Extending class by keeping unchanged parts, allowing code reusability</li>
                <li><strong>Classes organized in hierarchies</strong></li>
                <li><strong>Dynamic memory allocation and de-allocation</strong></li>
                <li><strong>Dynamic binding</strong></li>
                <li><strong>Polymorphism</strong></li>
            </ul>
        </div>

        <div class="subsection">
            <h3>Classes vs Objects</h3>
            <div class="key-points">
                <strong>Key Concepts:</strong>
                <ul>
                    <li><strong>Class:</strong> Defines data type based on data and functions that operate on it</li>
                    <li><strong>Object:</strong> Instance of a class that represents the data and its functions</li>
                    <li><strong>Analogy:</strong> Class is like a blueprint, objects are houses built from blueprint</li>
                    <li><strong>Focus:</strong> Object-oriented programming focuses on objects containing data and operations</li>
                </ul>
            </div>
        </div>

        <div class="subsection">
            <h3>Programming Paradigm Comparison</h3>
            <div class="memory-diagram">
                <strong>Program = Algorithm + Data</strong><br><br>
                Imperative Paradigm: Process (steps) of data manipulation<br>
                Object-Oriented Paradigm: Objects of the manipulation
            </div>
        </div>
    </div>

    <div id="class-implementation" class="section">
        <h2 class="section-title">5. Class Definition & Implementation</h2>

        <div class="subsection">
            <h3>Class Definition Structure</h3>
            <div class="code-block">class Queue {
private:                    // Data almost always private
    int queue_size;
protected:                  // For family members (inheritance)
    int *buffer;
    int front;
    int rear;
public:                     // Member functions almost always public
    void enqueue(int v) {...}
    int dequeue(void) {...}
    bool compact(void);     // Can be implemented outside
};</div>
        </div>

        <div class="subsection">
            <h3>Access Specifiers</h3>
            <div class="key-points">
                <strong>Access Control Rules:</strong>
                <ul>
                    <li><span class="access-level public">public</span> <strong>Data Members:</strong> Almost always private (uphold OOP principles)</li>
                    <li><span class="access-level public">public</span> <strong>Member Functions:</strong> Almost always public (user-accessible)</li>
                    <li><span class="access-level private">private</span> <strong>Members:</strong> Accessible only by member functions and friends</li>
                    <li><span class="access-level protected">protected</span> <strong>Members:</strong> Accessible by member functions, friends, and derived classes</li>
                </ul>
            </div>
        </div>

        <div class="subsection">
            <h3>Implementation Options</h3>
            <div class="two-column">
                <div>
                    <h4>In-class Implementation</h4>
                    <div class="code-block">class Queue {
public:
    int getRear() { return rear; }      // Getter
    void setRear(int value) {           // Setter
        rear = value; 
    }
};</div>
                    <p><strong>Advantage:</strong> More efficient for short functions</p>
                </div>
                <div>
                    <h4>Out-class Implementation</h4>
                    <div class="code-block">void Queue::enqueue(int v) {
    if (rear < queue_size)
        buffer[rear++] = v;
    else
        cout << "Need to compact queue" << endl;
}</div>
                    <p><strong>Advantage:</strong> Structurally clearer separation</p>
                </div>
            </div>
        </div>

        <div class="subsection">
            <h3>Scope Resolution Operator (::)</h3>
            <div class="formula">
                class_name::function_name
            </div>
            <p>Used when implementation of member functions are separated from class definition.</p>
            <div class="code-block">void Queue::enqueue(int v) {    // Implementation outside class
    if (rear < queue_size)
        buffer[rear++] = v;
    else
        cout << "Need to compact queue before inserting" << endl;
}</div>
        </div>

        <div class="subsection">
            <h3>Class as Data Type</h3>
            <div class="key-points">
                <strong>Class Usage:</strong>
                <ul>
                    <li>Class is full-fledged type (like int, double, etc.)</li>
                    <li>Can have variables of class type (called "objects")</li>
                    <li>Can have parameters of class type (pass-by-value, pass-by-reference)</li>
                    <li>Can use class type like any other type</li>
                </ul>
            </div>

            <div class="code-block">// Class Declaration and Usage
Queue MyQueue;                    // Stack allocation
Queue *MyQueuePtr = new Queue();  // Heap allocation

MyQueue.buffer = new int[5];      // Direct access
MyQueue.enqueue(10);

MyQueuePtr->buffer = new int[5];  // Pointer access
MyQueuePtr->enqueue(10);</div>
        </div>
    </div>

    <div id="access-control" class="section">
        <h2 class="section-title">6. Access Control & Information Hiding</h2>

        <div class="subsection">
            <h3>Information Hiding Levels</h3>
            <table class="comparison-table">
                <thead>
                    <tr>
                        <th>Access Level</th>
                        <th>Accessible By</th>
                        <th>Usage</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><span class="access-level public">public</span></td>
                        <td>Any functions (even outside class)</td>
                        <td>Interface functions</td>
                    </tr>
                    <tr>
                        <td><span class="access-level protected">protected</span></td>
                        <td>Member functions, friends, derived classes</td>
                        <td>Inheritance support</td>
                    </tr>
                    <tr>
                        <td><span class="access-level private">private</span></td>
                        <td>Member functions and friends only</td>
                        <td>Internal data and helper functions</td>
                    </tr>
                </tbody>
            </table>
        </div>

        <div class="subsection">
            <h3>Best Practices</h3>
            <div class="warning-box">
                <h4>Design Guidelines:</h4>
                <ul>
                    <li><strong>Data Members:</strong> Almost always private to hide data from user</li>
                    <li><strong>Member Functions:</strong> Almost always public (unless helper functions)</li>
                    <li><strong>Organization:</strong> Place public first for easy viewing by programmers</li>
                    <li><strong>Private Data:</strong> "Hidden" so irrelevant to users</li>
                </ul>
            </div>
        </div>
    </div>

    <div id="practical-examples" class="section">
        <h2 class="section-title">7. Practical Examples</h2>

        <div class="subsection">
            <h3>Complete Queue Class Implementation</h3>
            <div class="code-block">class Queue {
private:
    int queue_size;
protected:
    int *buffer;
    int front;
    int rear;
public:
    // Constructors
    Queue(int n) {
        front = 0; rear = 0;
        queue_size = n;
        buffer = new int[queue_size];
    }
    
    // Destructor
    ~Queue() {
        delete buffer;
    }
    
    // Member functions
    void enqueue(int value);
    int dequeue();
    bool compact(void);
};</div>
        </div>

        <div class="subsection">
            <h3>Circular (Cyclic) Queue</h3>
            <div class="memory-diagram">
                Empty Queue: front == rear<br>
                ┌─────┬─────┬─────┬─────┬─────┐<br>
                │  0  │  1  │  2  │ n-2 │ n-1 │<br>
                └─────┴─────┴─────┴─────┴─────┘<br>
                      ↑<br>
                  front/rear<br><br>
                Full Queue: (rear + 1) % n == front<br>
                ┌─────┬─────┬─────┬─────┬─────┐<br>
                │ ● ● │ ● ● │ ● ● │ ● ● │ ● ● │<br>
                └─────┴─────┴─────┴─────┴─────┘<br>
                  ↑                       ↑<br>
                front                   rear
            </div>
            <div class="key-points">
                <strong>Circular Queue Benefits:</strong>
                <ul>
                    <li>Each queue operation may take slightly longer time</li>
                    <li>No penalty when queue is falsely full</li>
                    <li>Better memory utilization</li>
                </ul>
            </div>
        </div>

        <div class="subsection">
            <h3>Time Class Example</h3>
            <div class="code-block">#include <iostream>
using namespace std;

class Time {
public:
    Time();                          // default constructor
    void setTime(int, int);          // set hour, minute
    void printMilitary();           // print military time format
    void printStandard();           // print standard time format
private:
    int hour;                       // 0 - 23
    int minute;                     // 0 - 59
};</div>
        </div>

        <div class="subsection">
            <h3>Memory Leak Detection Exercise</h3>
            <div class="warning-box">
                <h4>Common Memory Leak Scenarios:</h4>
                <ol>
                    <li><strong>Missing free()/delete:</strong> Memory allocated but never freed</li>
                    <li><strong>Lost pointers:</strong> Overwriting pointer before freeing memory</li>
                    <li><strong>Exception handling:</strong> Memory allocated before exception thrown</li>
                    <li><strong>Circular references:</strong> Objects referencing each other</li>
                </ol>
            </div>

            <div class="code-block">// Example: Memory leak in insertion
void insertion() {
    struct actor *c; char *n;
    c = malloc(sizeof(struct actor));
    if (c == 0) {
        printf("out of memory\n"); return -1;
    }
    n = get_name();           // ← Memory leak if get_name() allocates
    strcpy(c->name, n);       // ← Should free(n) here!
    c->next = head;
    head = c;
    return 1;
}</div>
        </div>
    </div>

    <div style="text-align: center; margin-top: 40px; padding: 20px; background-color: #fef9e7; border-radius: 10px;">
        <p style="color: #e67e22; font-weight: bold;">CSE 240 - Introduction to Programming Languages | Arizona State University</p>
        <p style="color: #666; font-size: 0.9em;">This cheat sheet covers Module 6: Memory Management and Object-Oriented Programming</p>
    </div>
</body>
</html>