| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"puzzlecracker" |
| Date: |
08 Aug 2005 07:39:23 PM |
| Object: |
question abot Exceptional C++ (47. eng puzzles) Iby Sutter |
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.
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: question abot Exceptional C++ (47. eng puzzles) Iby Sutter |
09 Aug 2005 08:47:50 AM |
|
|
puzzlecracker wrote:
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
.
|
|
|
|

|
Related Articles |
|
|