Listing 1 This program illustrates how executing longjmp can cause a memory leak when explicitly allocating arrays.

#include <assert.h>
#include <iostream.h>
#include <setjmp.h>

jmp_buf env;

void f(int n)
   {
   // ...
   if (n < 3)
      longjmp(env, 1);
   // ...
   }

void g(int n)
   {
   float *a = new float[n];
   assert(a != 0);
   // ...
   f(n);
   // ...
   delete [] a;
   }

int main()
   {
   if (setjmp(env) != 0)
      {
      // recover from error ...
      }
   int n;
   cout << "n ?";
   cin >> n;
   g(n);
   return 0;
   }
/* End of File  */