23. Telemetry Packet Memory Cleanup

#include <iostream>
using namespace std;

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

    // Allocate first telemetry packet
    int* packet = new int;
    *packet = firstValue;

    // Delete old packet before reusing pointer
    delete packet;

    // Allocate second telemetry packet
    packet = new int;
    *packet = secondValue;

    // Print final packet value
    cout << *packet;

    // Final cleanup
    delete packet;

    return 0;
}

Explanation & Logic Summary:

  • Telemetry packets represent dynamically allocated data.
  • Heap pointers must be freed before reuse to prevent leaks.
  • Each allocation is paired with a corresponding delete.
  • This pattern is essential in memory-constrained firmware systems.

Firmware Relevance & Real-World Context:

  • Embedded telemetry systems often allocate packet buffers dynamically.
  • Memory leaks cause fragmentation, instability, and system resets.
  • On microcontrollers with limited RAM, proper memory lifecycle management is critical.
  • Correct use of new and delete ensures long-running, reliable firmware.

 

 

 

 

 

 

Loading...

Input

25 40

Expected Output

40