Amiga!
Home
Support
Get the SDK
(Answer) (Category) Amiga Inc. Faq-O-Matic : (Category) Amiga SDK FAQ :
Is there a bug in GCC and thus VPCC regarding class initialization?
The following code will not run under the SDK.  It crashes with the following error:

[shell: child terminated due to signal 7: SIGSEGV: Invalid memory access]

Here is the code taken from 'Learn C++ in 21 Days'.

//Listing 6.4

#include iostream.h

class Cat {
  public:
    Cat(int initialAge);
    ~Cat();
    int GetAge();
    void SetAge(int age);
    void Meow();
  private:
    int itsAge;
};


Cat::Cat(int initialAge)

{

itsAge = initialAge;

}


Cat::~Cat()

{
}


int Cat::GetAge()

{

return(itsAge);

}


void Cat::SetAge(int age)

{

itsAge = age;

}


void Cat::Meow()

{

cout << "Meow.\n";

}


int main()

{

Cat Frisky(5);

Frisky.Meow();

cout << "Frisky is a cat who is " << Frisky.GetAge() << " years old.\n";

Frisky.Meow();

Frisky.SetAge(7);

cout << "Now Frisky is " << Frisky.GetAge() << " years old.\n";

return(0);

}

The line from main() 'Cat Frisky(5);' is where the problem lies.  It seems the initialization of 5 is never executed, or the whole constructor is never called.
I compiled the same program using g++, I get almost the same result in that the code will run, however the first use of the GetAge() method returns a huge number, decidely NOT 5.

I also compiled the code under SAS/C++ and Codewarrior for Windows.  Both worked as expected.
As there seems to be no one from Amiga giving answers to all the questions in this faq, i tell You what I found out: Did You try the Compiler Option -fno-exceptions ? This did help in my case! As mentioned in the manuals, the C++ Exceptions still have bugs! And these bugs seem to be of bigger nature...
Hey, I grabbed your example and compiled it with G++ and vpcc. I was able to reproduce your problems in the Amiga SDK. It seems to dump, even in very, very simple C++ code.
The problem seems to be related to the destructor. Take the destructor out of your cat (it doesn't really do anything in this example anyway), and the program will run happily.
It does, however, currently make it virtually impossible to implement any C++ apps on the SDK.
This is not a problem with G++ or gcc.
LT. http://www.redloop.com/larsde/
[Append to This Answer]
stricq, robert.kocher, larsde
Previous: (Answer) How can I change the size of the intent_media-window ?
Next: (Answer) The Amiga SDK installer will not accept my Developer ID
This document is: http://www.amiga.com/cgi-bin/fom.cgi?file=105
[Search] [Appearance]
This is a Faq-O-Matic 2.710.
This FAQ administered by ray@amiga.com


© 1996-2000 Amiga, Inc.