virtual functions stomping over the machine

Paul F. Johnson paulf.johnson at
Wed Dec 19 17:49:59 PST 2001


Below is a small program which compiles fine. If I run this a couple of
times, the machine becomes unstable with an eventual power down reset being

The first few times results in toolbox errors from MessengerPro, after
that, things become serious.

My suspicions on a problem came about when I could not compile cgicc with
the current sdk build and the machine remain stable afterwards. Okay, there
is a difference in that cgicc uses #defines as part of the prototype for
the virtual function, but given neither are stable...

I have tried the code compiled without optimisation and without running it
through squash (as well as optimised and with squash). Under GCC on the
linux box (not the cross compiler), the code works fine and does not upset
the system.

Could someone please try the code and tell me I'm imagining things?




#include <iostream>

using namespace std;

class animal {
  virtual void print() const // virtual print function
    cout << "Unknown animal type\n";
  virtual ~animal(){}
  int nlegs;

class fish:public animal {
  fish(int n) {nlegs = n;}
  void print() const
    cout << "A fish has " << nlegs << " legs.\n";

class bird:public animal {
  bird(int n) {nlegs = n;}
  void print() const
    cout << "A bird has " << nlegs << " legs.\n";

class mammal:public animal {
  mammal(int n) {nlegs = n;}
  void print() const
    cout << "A mammal has " << nlegs << " legs.\n";


#include "virtual.h"

int main()
  animal *p[4];
  int i;
  p[0] = new fish(0);
  p[1] = new bird(2);
  p[2] = new mammal(4);
  p[3] = new animal;
  for (i = 0; i < 4 ; ++i) p[i]->print();
  for (i = 0; i < 4 ; ++i) delete p[i];
  return 0;

Sent from the heart of British Technology at its best - the all
new RiscStation R7500 - now running with the new 50ns upgrade
All opinions expressed are solely mine and do not represent any
other persons or companies.

More information about the gcc mailing list