Telemetry Packet Memory Cleanup

#include <iostream>
using namespace std;

int main() {
    int firstValue, secondValue;
    cin >> firstValue >> secondValue;

    int* packet = new int;
    *packet = firstValue;

    // 2. CRITICAL: Delete the old packet before reusing the pointer
    // If we did 'packet = new int' without this, the memory at 'firstValue' 
    // would be lost forever in the Heap (a memory leak).
    delete packet;

    // 3. Allocate the second packet
    packet = new int;
    *packet = secondValue;

    // 4. Print the final packet value
    cout << *packet << endl;

    // 5. Final cleanup before exit
    delete packet;

    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

25 40

Expected Output

40