Segmentation fault during object creation.

Sergey V. Gomanuk sergg@kazbek.ispras.ru
Sun Dec 26 07:29:00 GMT 1999


Hi,

Version of compiler: 2.95.2
Machine: HP 712/100
Operating System: HP-UX 10.20

First example:

#include <iostream.h>

class A
{
public:
  A();
};

A::A() {
  cout << "A::A" << endl;
}

int main()
{
  cout << "main::1" << endl;

  A *a = new A;

  cout << "main::2" << endl;

  delete a;

  return 0;
}

end of example.

Result of working:
main::1
Segmentation fault (core dumped)

 Second example (same as first but without first "cout"):

#include <iostream.h>

class A
{
public:
  A();
};

A::A() {
  cout << "A::A" << endl;
}

int main()
{
  //  cout << "main::1" << endl;

  A *a = new A;

  cout << "main::2" << endl;

  delete a;

  return 0;
}

end of example.

It works:
A::A
main::2

Additional note:
Object creation is very unstable. Sometimes, replace like"A *a = new A;"
-> "A a;" can help.
To my mind,  object creation fault depends on its place in code.

Sergei Gomanyuk.




More information about the Gcc-bugs mailing list