Article View: gnu.g++.bug
Article #1933g++ misses constructor
From: George Beshers
Date: Tue, 02 May 1989 14:20
Date: Tue, 02 May 1989 14:20
85 lines
1648 bytes
1648 bytes
Consider nested functions calls where the first function calls
the second function from the return statement (I think this just
happens in the return statement). The nested function returns
a *reference* to an object which is passed back by *value*.
The constructor is not called and the value is deleted.
For example:
#include <stream.h>
#include <string.h>
struct Ref {
int ref;
char *p;
};
struct Str {
Ref *r;
Str();
Str(char *);
Str(Str &);
~Str();
friend ostream&
operator<<(ostream&, Str);
};
Str::Str()
{ r = 0; }
Str::Str(char *q)
{
r = new Ref;
r->ref = 1;
r->p = new char[strlen(q)];
strcpy(r->p, q);
}
Str::Str(Str &S)
{
r = S.r;
r->ref++;
}
Str::~Str()
{
if (r != 0) {
r->ref--;
if (r->ref == 0) {
delete r->p;
delete r;
}
r = 0;
}
}
ostream& operator<<(ostream& out, Str S)
{
out << S.r->p;
return out;
}
Str A;
Str & f1()
{
return A;
}
Str f2()
{
return f1();
}
main()
{
A = "Test\n";
cout << f2();
cout << A.r->ref << "\n";
}
Sun3/50 nfp
SunOS 3.5
C++ version 1.34.1 with most posted bug fixes.
George Beshers
Technical Details
Message-ID:
<8905021420.AA12914@brcsun2.brc.uconn.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ukma!tut.cis.ohio-state.edu!brcsun2.brc.uconn.edu!beshersShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ukma!tut.cis.ohio-state.edu!brcsun2.brc.uconn.edu!beshers From: beshers@brcsun2.brc.uconn.edu (George Beshers) Newsgroups: gnu.g++.bug Subject: g++ misses constructor Message-ID: <8905021420.AA12914@brcsun2.brc.uconn.edu> Date: 2 May 89 14:20:27 GMT Sender: daemon@tut.cis.ohio-state.edu Distribution: gnu Organization: GNUs Not Usenet Lines: 85