Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Custom allocator sample code for vector

Reply
Thread Tools

Custom allocator sample code for vector

 
 
Alex Vinokur
Guest
Posts: n/a
 
      08-06-2004
I am looking for any custom allocator sample code for std::vector.

Thanks.

--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn





 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      08-06-2004

"Alex Vinokur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I am looking for any custom allocator sample code for std::vector.
>


http://www.josuttis.com/libbook/memo...lloc1.cpp.html


 
Reply With Quote
 
 
 
 
Alex Vinokur
Guest
Posts: n/a
 
      08-06-2004

"Sharad Kala" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> "Alex Vinokur" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I am looking for any custom allocator sample code for std::vector.
> >

>
> http://www.josuttis.com/libbook/memo...lloc1.cpp.html
>
>


Thanks.

In that sample we can see that methods 'allocate' and 'deallocate' were invoked.
But methods 'construct' and 'destroy' were not invoked.
When should 'construct' and 'destroy' be invoked?


--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn





 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      08-07-2004

"Alex Vinokur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> When should 'construct' and 'destroy' be invoked?


An allocator has to provide a construct and destruct operation. You may want
to take a look at 20.1.5 (Allocator requirements).

Construct is used to initialize the memory allocated with a value. This is
basically a call to placement new. So basically construct is invoked when an
object is created in the container.
Destroy simply calls the destructor for the object.

-Sharad



 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      08-07-2004

"Sharad Kala" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> "Alex Vinokur" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > When should 'construct' and 'destroy' be invoked?

>
> An allocator has to provide a construct and destruct operation. You may want
> to take a look at 20.1.5 (Allocator requirements).
>
> Construct is used to initialize the memory allocated with a value. This is
> basically a call to placement new. So basically construct is invoked when an
> object is created in the container.


For instance, v.push_back (element) for vector?

However, in http://www.josuttis.com/libbook/memo...lloc1.cpp.html construct isn't invoked while push_back invocation.

> Destroy simply calls the destructor for the object.
>
> -Sharad
>
>
>



--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      08-07-2004

"Alex Vinokur" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> "Sharad Kala" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> >
> > "Alex Vinokur" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> > > When should 'construct' and 'destroy' be invoked?

> >
> > An allocator has to provide a construct and destruct operation. You may want
> > to take a look at 20.1.5 (Allocator requirements).
> >
> > Construct is used to initialize the memory allocated with a value. This is
> > basically a call to placement new. So basically construct is invoked when an
> > object is created in the container.

>
> For instance, v.push_back (element) for vector?
>
> However, in http://www.josuttis.com/libbook/memo...lloc1.cpp.html construct isn't invoked while push_back invocation.
>
> > Destroy simply calls the destructor for the object.
> >
> > -Sharad
> >


[snip]


Compiler g++ 3.3.1 (cygming special).
Here is a fragment from file stl_alloc.h.

/**
* @defgroup Allocators Memory Allocators
* @if maint
* stl_alloc.h implements some node allocators. These are NOT the same as
* allocators in the C++ standard, nor in the original H-P STL. They do not
* encapsulate different pointer types; we assume that there is only one
* pointer type. The C++ standard allocators are intended to allocate
* individual objects, not pools or arenas.
*
* In this file allocators are of two different styles: "standard" and
* "SGI" (quotes included). "Standard" allocators conform to 20.4. "SGI"
* allocators differ in AT LEAST the following ways (add to this list as you
* discover them):
*
* - "Standard" allocate() takes two parameters (n_count,hint=0) but "SGI"
* allocate() takes one paramter (n_size).
* - Likewise, "standard" deallocate()'s argument is a count, but in "SGI"
* is a byte size.
* - max_size(), construct(), and destroy() are missing in "SGI" allocators.
* - reallocate(p,oldsz,newsz) is added in "SGI", and behaves as
* if p=realloc(p,newsz).
*
* "SGI" allocators may be wrapped in __allocator to convert the interface
* into a "standard" one.
* @endif
*
* @note The @c reallocate member functions have been deprecated for 3.2
* and will be removed in 3.4. You must define @c _GLIBCPP_DEPRECATED
* to make this visible in 3.2; see c++config.h.
*
* The canonical description of these classes is in docs/html/ext/howto.html
* or online at http://gcc.gnu.org/onlinedocs/libstd...t/howto.html#3
*/

Perhaps, MyAlloc in the Josuttis' sample works as "SGI" allocator which doesn't contain construct(), and destroy() (?).


--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      08-07-2004

"Alex Vinokur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> For instance, v.push_back (element) for vector?


Yes

> However, in http://www.josuttis.com/libbook/memo...lloc1.cpp.html

construct isn't invoked while push_back invocation.

Try putting a breakpoint in Construct and then see.

-Sharad



 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      08-07-2004

"Sharad Kala" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> "Alex Vinokur" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > For instance, v.push_back (element) for vector?

>
> Yes
>
> > However, in http://www.josuttis.com/libbook/memo...lloc1.cpp.html

> construct isn't invoked while push_back invocation.
>
> Try putting a breakpoint in Construct and then see.
>


I put assert(0) in construct().
No response.

// initialize elements of allocated storage p with value value
void construct (pointer p, const T& value)
{
// initialize memory with placement new
new((void*)p)T(value);
assert (0); // Added by me
}

[snip]


--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      08-07-2004

>
> I put assert(0) in construct().
> No response.


Is NDEBUG macro defined ? I can very much see the assert failure on MS VC 7
if I insert the above line (in debug build). Try putting cout << "In
construct" and then see.


 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      08-07-2004

"Sharad Kala" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> >
> > I put assert(0) in construct().
> > No response.

>
> Is NDEBUG macro defined ? I can very much see the assert failure on MS VC 7
> if I insert the above line (in debug build). Try putting cout << "In
> construct" and then see.
>
>


Done: cout << "In construct" has been put.
No response too.
It seems that construct() is not invoked.


--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vector<T> v.s. vector<T,allocator> z65536 C++ 4 07-06-2010 04:55 PM
undefined reference to `std::vector<int, std::allocator<int> > rethnor@gmail.com C++ 3 11-06-2007 04:38 PM
Behaviour of custom allocator for vector Alex Vinokur C++ 3 04-01-2006 04:19 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
std::vector allocator? Gernot Frisch C++ 4 07-21-2004 09:30 AM



Advertisments