Sample C++ routine that generates a Language Environment dump

Figure 1 shows a sample C++ routine that uses a protection exception to generate a dump.

Figure 1. Example C++ routine with protection exception generating a dump
#include <iostream.h>
#include <ctest.h>
#include "stack.h"

int main() {
  cout << "Program starting:\n";
  cerr << "Error report:\n";

  Stack<int> x;
  x.push(1);
  cout << "Top value on stack : " << x.pop() << '\n';
  cout << "Next value on stack: " << x.pop() << '\n';
  return(0);
}

Figure 2 shows the template file stack.c

Figure 2. Template file STACK.C
#ifndef __STACK__
  #include "stack.h"
#endif

template <class T> T Stack<T>::pop() {
  T value = head->value;
  head = head->next;

  return(value);
}
template <class T> void Stack<T>::push(T value) {
  Node* newNode  = new Node;
  newNode->value = value;
  newNode->next  = head;
  head = newNode;
}

Figure 3 shows the header file stack.h.

Figure 3. Header file STACK.H
#ifndef __STACK_
  #define __STACK__
  template <class T> class Stack {
    public:
      Stack() {
        char* badPtr = 0; badPtr -= (0x01010101);
        head = (Node*) badPtr; /* head initialized to 0xFEFEFEFF */
      }
      T pop();
      void push(T);
    private:
      struct Node {
        T value;
        struct Node* next;
      }* head;
  };
#endif