delorie.com/djgpp/bugs/show.cgi   search  
Bug 000086

When Created: 06/03/1996 20:56:06
Against DJGPP version: 2.00
By whom: peters.88@osu.edu
Abstract: I found 4 bugs which I have documented in an example (bugs.cc)
1.) The compiler doesn't automatically generate the default
    terminate() function.
2.) The compiler doesn't accept arguments which are referances
    to pointers.
3.) The compiler doesn't properly handle inheritance as it
    pertains to exceptions.  I attempted to catch an exception
    via a referance to its base-class.
4.) The compiler doesn't properly initialize variables within
    constructors.

Note added: 06/03/1996 20:58:13
By whom:
/*
 * BUGS.CC
 *
 * Dale L. Peters, II
 * 06/03/96
 *
 * This program contains several examples of valid code which does
 * not compile correctly.
 *
 * This code was compiled using the DJGPP 2.0 C++ compiler.
 */

// conditional-compilation flags
//#define TEST1

// header files
#include <iostream.h>

// type definitions
struct A {
  A() { }
  virtual void error() { cout << "Generic exception" << "\n"; }
};
struct exception_1 : A {
  exception_1() { }
  void error() {
    cout << "An exception of type exception_1 has been thrown." << "\n";
  }
};
struct exception_2 : A {
  exception_2() { }
};
class B {
  int b;

  public:
  B(int i=0) : b(i) { cout << "Within B:  b = " << b << ".\n"; }
};
class C : public B {
  int i;

  public:
  C() : i(1), B(2) { cout << "Within C:  i = " << i << ".\n"; }
};
class D : public B {
  int i;

  public:
  D() : i(3), B(i) { cout << "Within D:  i = " << i << ".\n"; }
};
class E : public B {
  int i;

  public:
  E() : i(4) { cout << "Within E:  i = " << i << ".\n"; }
};

// function prototypes
void start(),
     terminate();  // the compiler should have generated terminate()
#if defined(TEST1)
void f(char& *p);  // the compiler doesn't accept this type of parameter
#endif  // TEST1

void main() {
  try { start(); }
  catch(exception_2& e2) { e2.error(); }
  catch(A& e1) { e1.error(); }
  catch(...) {     // the compiler doesn't properly handle inheritance
    B b(-1);       // with respect to exceptions (because exception_1
    C c;           // is-a A--exception_1 should have been caught by e1)
    D d;           // D::B.b is not initialized properly
    E e;
#if defined(TEST1)
    char *c;
    f(c);
#endif  // TEST1
  }
}  // end of main

#if defined(TEST1)
void f(char& *p) {
  char c[]="This is a test.";

  p=new char[sizeof(c)];
  for(i=0;  i<sizeof(c);  i++) p[i]=c[i];
}  // end of f
#endif  // TEST1

void start() { throw exception_1(); }

void terminate() { }

Workaround added: 06/03/1996 21:26:29
By whom:
1.) For the referance-to-a-pointer-parameter problem:  1.) pass
    the address of the pointer (in the caller), 2.) add another
    level of indirection (in the function definition).

i.e.

    void main() {
      char *p;
      void f(char **p);

      f(&p);
    }

    void f(char **p) { . . . }

2.) For the inheritance/exception problem, select for each
    individual exception.

3.) For the constructor/initialization problem, either use
    another class to separate data member and any other kind
    of sub-object initialization, or initialize the data members
    before entering the body of the constructor and initialize
    the other sub-objects within the body of the constructor.

Note added: 01/24/1998 20:53:02
By whom: quantum@mail.idigital.net
Just a note.. I've experienced a similiar problem with classes.. 
One worka around that seems to work for constructor problems is to switch the datatypes around inside the class definitions. Maybe its just coincidence, but I found that an integer following an array of some sort often does not initialize correct.
e.g class wrt {
                int array[NUM_ICONS];
                int eg;
               };
in those cases eg often doesn't initialize properly. (It doesn't happen all the time)
But if the order is reversed
e.g. class wrt {
                 int eg;
                 int array[NUM_ICONS];
               };
I find the problem is usually fixed.

Closed on 04/13/1999 08:00:24: A problem with the C++ compiler, not a DJGPP bug.
By whom: eliz@is.elta.co.il



  webmaster     delorie software   privacy  
  Copyright © 2010   by DJ Delorie     Updated Jul 2010