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



 DEVELOP > c-Plus-Plus > question abot Exceptional C++ (47. eng puzzles) Iby Sutter

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER