24. Reset Fault Buffer

#include <iostream>
using namespace std;

int main() {
    int code;
    cin >> code;

    int* faultBuf = new int;   // dynamic allocation
    *faultBuf = code;

    cout << *faultBuf << " "; // print stored code

    delete faultBuf;          // free memory
    faultBuf = nullptr;       // reset pointer

    if (faultBuf == nullptr)
        cout << "OK";
    else
        cout << "NOT_OK";

    return 0;
}

Explanation & Logic Summary:

  • Dynamic memory stores the fault code temporarily.
  • Memory is freed using delete.
  • Pointer reset prevents accidental reuse or double delete.
  • Final check ensures the pointer was properly cleared.

Firmware Relevance & Real-World Context:

  • Fault buffers and diagnostic logs are often allocated dynamically in embedded firmware.
  • Resetting pointers after deletion avoids undefined behavior and memory corruption.
  • This pattern appears in watchdog handlers, error recovery logic, and diagnostic modules.

 

 

 

 

 

Loading...

Input

55

Expected Output

55 OK