help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: lifetime of temporaries


From: Maxim Yegorushkin
Subject: Re: lifetime of temporaries
Date: Fri, 03 Aug 2007 02:32:25 -0700
User-agent: G2/1.0

On 5 Jul, 13:28, sebastien...@gmail.com wrote:
> On Jul 4, 3:44 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
> wrote:
>
>
>
> > On 4 Jul, 16:55, sebastien...@gmail.com wrote:
>
> > > I have the following program:
>
> > > class A
> > > {
> > >   string s;
>
> > >   public:
> > >   A(const string& a) : s(a) { cout << s << endl; }
>
> > > };
>
> > > class B : public A
> > > {
> > >   public:
> > >   B(const string& b) : A(b) { }
>
> > > };
>
> > > class C : public B
> > > {
> > >   public:
> > >   C() : B(string("ABC")) { }
>
> > > };
>
> > > int main(int argc, char *argv)
> > > {
>
> > > unsigned char i = 0;
>
> > > C* c = new C;
>
> > > delete c;}
>
> > > /////////////////////////////////////////////////////////
>
> > > If I put the delete c statement in comment, valgrind will report that
> > > I'm leaking c AND the string temporary created when invoking B ctor.
> > > why ?
>
> > GNU C++ std::basic_string<> uses reference counting and copy-on-write.
> > It means that the memory allocated by the temporary is shared by A::s.
> > When destructor of A::s is not called it leaks memory, and that memory
> > was allocated by the temporary.
>
> Darn optimization ! ;-)
>
> Thanks for the explanation, it all make sense now. Got any reference
> on the web on that and other similar feature implemented in g++ ?

You might find useful GNU C++ library documentation:
http://gcc.gnu.org/onlinedocs/libstdc++/documentation.html



reply via email to

[Prev in Thread] Current Thread [Next in Thread]