By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,157 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,157 IT Pros & Developers. It's quick & easy.

Construct On First Use Idiom example from FAQ

P: n/a
The FAQ gives the following example:

Fred& x()
{
static Fred* ans = new Fred();
return *ans;
}

My question is:
What will happen if this function is called more than once?
Will a new object be created?
Will new memory be allocated?
Will the memory for the first object be leaked? (we no longer have pointer
to it)

Aug 10 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
k.w. wrote:
The FAQ gives the following example:

Fred& x()
{
static Fred* ans = new Fred();
return *ans;
}

My question is:
What will happen if this function is called more than once?
What exactly do you mean?
Will a new object be created?
Only the first time.
Will new memory be allocated?
Again, only the first time.
Will the memory for the first object be leaked? (we no longer have
pointer to it)
If you never deallocate it (which is probably true, it's hard to
pick the right time to call 'delete'), then yes, the first (and
the only) object represents a memory leak. Probably insignificant
in the whole schema of things.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 10 '07 #2

P: n/a
>The FAQ gives the following example:
>>
Fred& x()
{
static Fred* ans = new Fred();
return *ans;
}

My question is:
What will happen if this function is called more than once?

What exactly do you mean?

It seems that the FAQ says that this function can be called a couple of
times and it will create only one object and return a reference to it. How
is this possible? Cause every time this function is called there is a call
to operator new also.
Aug 10 '07 #3

P: n/a
k.w. wrote:
>>The FAQ gives the following example:

Fred& x()
{
static Fred* ans = new Fred();
return *ans;
}

My question is:
What will happen if this function is called more than once?

What exactly do you mean?


It seems that the FAQ says that this function can be called a couple
of times and it will create only one object and return a reference to
it. How is this possible? Cause every time this function is called
there is a call to operator new also.
A static object is *initialised* at most once during the execution
of the program. That's how static object work. What book are you
reading that doesn't explain that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 10 '07 #4

P: n/a
Hi!

Victor Bazarov schrieb:
If you never deallocate it (which is probably true, it's hard to
pick the right time to call 'delete'), then yes, the first (and
the only) object represents a memory leak. Probably insignificant
in the whole schema of things.

Anyway, you can make it better:

Fred& x()
{
static Fred ans;
return ans;
}

The static instance will only be constructed once. And it will
automatically destructed at the end of the program. No leakage. No
possible failure of allocation.

Frank
Aug 10 '07 #5

P: n/a

k.w. <me@home.plwrote in message...

Yes. And there are more concepts of C++ like that that you simply
won't find in a reference manual. Do not waste your time, go get
a copy of "Accelerated C++" *now*, while some technical bookstore
is still open, if not, do it tomorrow.
V

OK, I'll do that :) thank you
While you are looking for a book, this may interest you:

Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

--
Bob R
POVrookie
Aug 10 '07 #6

P: n/a
On Aug 10, 3:17 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
k.w. wrote:
The FAQ gives the following example:
Fred& x()
{
static Fred* ans = new Fred();
return *ans;
}
My question is:
What will happen if this function is called more than once?
[...]
Will the memory for the first object be leaked? (we no longer have
pointer to it)
If you never deallocate it (which is probably true, it's hard to
pick the right time to call 'delete'), then yes, the first (and
the only) object represents a memory leak. Probably insignificant
in the whole schema of things.
That really depends on your definition of a "leak". The
definition I've always heard is memory which isn't freed, but
that can't be accessed. By that definition, this isn't a leak,
at least not as long as you can call x.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 11 '07 #7

P: n/a
James Kanze wrote:
On Aug 10, 3:17 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>k.w. wrote:
>>The FAQ gives the following example:
>>Fred& x()
{
static Fred* ans = new Fred();
return *ans;
}
>>My question is:
What will happen if this function is called more than once?

[...]
>>Will the memory for the first object be leaked? (we no longer have
pointer to it)
>If you never deallocate it (which is probably true, it's hard to
pick the right time to call 'delete'), then yes, the first (and
the only) object represents a memory leak. Probably insignificant
in the whole schema of things.

That really depends on your definition of a "leak". The
definition I've always heard is memory which isn't freed, but
that can't be accessed. By that definition, this isn't a leak,
at least not as long as you can call x.
That's within the same program. What if the hosting environment does
not reclaim dynamic (free store) memory unless it's explicitly 'freed'?
If 'delete' is never called, the hosting environment has no idea that
the memory is accessible and will never reuse it. That's a leak. And
if there exists a definition of a leak that is legitimate, then in
general, there is a leak, at least in my book.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 12 '07 #8

P: n/a
On Aug 12, 5:21 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
On Aug 10, 3:17 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
k.w. wrote:
The FAQ gives the following example:
>Fred& x()
{
static Fred* ans = new Fred();
return *ans;
}
>My question is:
What will happen if this function is called more than once?
[...]
>Will the memory for the first object be leaked? (we no longer have
pointer to it)
If you never deallocate it (which is probably true, it's hard to
pick the right time to call 'delete'), then yes, the first (and
the only) object represents a memory leak. Probably insignificant
in the whole schema of things.
That really depends on your definition of a "leak". The
definition I've always heard is memory which isn't freed, but
that can't be accessed. By that definition, this isn't a leak,
at least not as long as you can call x.
That's within the same program.
I didn't say that.
What if the hosting environment does
not reclaim dynamic (free store) memory unless it's explicitly 'freed'?
Then the hosting environment is broken, and you can't use a
number of useful idioms on it. I don't know of any "hosted"
environment where this is the case, however.
If 'delete' is never called, the hosting environment has no idea that
the memory is accessible and will never reuse it. That's a leak. And
if there exists a definition of a leak that is legitimate, then in
general, there is a leak, at least in my book.
I'm having trouble parsing that last sentence. If the hosting
environment doesn't reclaim all of the resources used by your
program when the program exits, then the hosting environment
leaks resources. No disagreement there.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 12 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.