optimization/4641: Temporary is not destructed when -O is used with recursive fn.

b.bull@niwa.cri.nz b.bull@niwa.cri.nz
Sun Oct 21 23:46:00 GMT 2001


>Number:         4641
>Category:       optimization
>Synopsis:       Temporary is not destructed when -O is used with recursive fn.
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Sun Oct 21 23:46:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Brian Bull
>Release:        gcc version 2.95.3 20010315 (release) [FreeBSD]
>Organization:
>Environment:

>Description:
Small example containing construction of a temporary as 
 a return value of a recursive function 'f'.

With -O, the temporary is never destructed.
Without -O, or if the code is rewritten to avoid
  recursion, the temporary is destructed correctly.

Thanks in advance for looking at this.
>How-To-Repeat:
g++ example.C -o example
example 
g++ example.C -o example -O
example 
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="example.C"
Content-Disposition: inline; filename="example.C"

#include <stream.h>

class A{
public:
  double x;
  ~A(){cerr << "~A\n";}
  A(double d){x = d; cerr << "A::A (double)\n";}
  A(const A& a){x = a.x; cerr << "A::A (A)\n";}
};

A operator+(const A& a,double d){
  return a.x+d;
}

double f(const A& a){
  if (a.x==1){
    return f(a+1);  // the temporary created as the result of 'a+1' is never destructed if -O is used
                    // but without -O, or if we rewrite the program to avoid recursion,
                    // the destructor is called correctly
  } else {
	return a.x;
  }
}

void main(){
  A a(1);
  f(a);
}



More information about the Gcc-prs mailing list