http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50373
Bug #: 50373
Summary: Incorrect Code generated for function with two
possible return values (Seg Fault)
Classification: Unclassified
Product: gcc
Version: 4.5.2
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
The following program, when run, generates a seg fault if the value of Q is
nonzero. It looks like the compiler is failing to create a copy of the
returned value, and is trying to free the memory twice.
#include <iostream>
using namespace std;
class testclass {
public:
testclass() {
data = new int[5];
}
~testclass() {
delete[] data;
}
private:
int* data;
};
testclass func(int Q) {
testclass A;
testclass B;
if(Q) {
return A;
} else {
return B;
}
}
int main() {
int Q = 1; //runs ok if Q=0;
func(Q);
return 0;
}