question abot Exceptional C++ (47. eng puzzles) Iby Sutter

P

puzzlecracker

Item 30 page 115:

template <size_t S>
class FixedAllocator
{
public:
void* Allocate (/*requested size is always S*/);
void Deallocate (void *);
private:
/*implemented using statics*/

};
"... Because the private details are likely to use statics,however,
there could be problems if Deallocate is ever called from a static
object's destructor. Probably safer is a singleton that manages a
sepearate free list..."

First of all how is it possible to ahave a static object?

Why would there be a problem, where (could someone please provide an
example)?


Thanks.
 
V

Victor Bazarov

puzzlecracker said:
Item 30 page 115:

template <size_t S>
class FixedAllocator
{
public:
void* Allocate (/*requested size is always S*/);
void Deallocate (void *);
private:
/*implemented using statics*/

};
"... Because the private details are likely to use statics,however,
there could be problems if Deallocate is ever called from a static
object's destructor. Probably safer is a singleton that manages a
sepearate free list..."

First of all how is it possible to ahave a static object?

Uh... What do you mean? Any object *defined* at a namespace scope has
static storage duration. Any object defined in other scopes but declared
'static' also has static storage duration. Those are static objects.
Why would there be a problem, where (could someone please provide an
example)?

The problem is the (unspecified) order of initialisation (and destruction)
of static objects. If whatever storage 'FixedAllocator' uses is created
_later_ than the object whose destructor is going to use 'FixedAllocator',
then 'FixedAllocator's storage is going to be destroyed before the d-tor
of the other static object is invoked, so the d-tor is going to try to use
the part of 'FixedAllocator' that doesn't exist any longer.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top