473,414 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Custom allocator sample code for vector

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

Jul 22 '05 #1
15 4239

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n***********@uni-berlin.de...
I am looking for any custom allocator sample code for std::vector.


http://www.josuttis.com/libbook/memo...lloc1.cpp.html
Jul 22 '05 #2

"Sharad Kala" <no******************@yahoo.com> wrote in message news:2n***********@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n***********@uni-berlin.de...
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

Jul 22 '05 #3

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n************@uni-berlin.de...
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

Jul 22 '05 #4

"Sharad Kala" <no******************@yahoo.com> wrote in message news:2n************@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n************@uni-berlin.de...
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

Jul 22 '05 #5

"Alex Vinokur" <al****@big-foot.com> wrote in message news:2n************@uni-berlin.de...

"Sharad Kala" <no******************@yahoo.com> wrote in message news:2n************@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n************@uni-berlin.de...
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

Jul 22 '05 #6

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n************@uni-berlin.de...

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

Jul 22 '05 #7

"Sharad Kala" <no******************@yahoo.com> wrote in message news:2n************@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n************@uni-berlin.de...

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


Jul 22 '05 #8

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.
Jul 22 '05 #9

"Sharad Kala" <no******************@yahoo.com> wrote in message news:2n************@uni-berlin.de...

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

Jul 22 '05 #10
On Fri, 6 Aug 2004 18:56:06 +0300, "Alex Vinokur"
<al****@big-foot.com> wrote:

"Sharad Kala" <no******************@yahoo.com> wrote in message news:2n***********@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n***********@uni-berlin.de...
> 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?


They may never be invoked - containers are not required to call them.
I believe that Dinkumware's lib does call them, since they support
"unusual" pointer types and memory models. I doubt anyone else does.

Tom
Jul 22 '05 #11
They may never be invoked - containers are not required to call them.
I believe that Dinkumware's lib does call them, since they support
"unusual" pointer types and memory models. I doubt anyone else does.


Why are they then as part of standard in allocator requirements in 20.1.5 ?
Is it an optional feature or are the compilers not being conformant ?
Jul 22 '05 #12
On Wed, 11 Aug 2004 14:45:24 +0530, "Sharad Kala"
<no******************@yahoo.com> wrote:
They may never be invoked - containers are not required to call them.
I believe that Dinkumware's lib does call them, since they support
"unusual" pointer types and memory models. I doubt anyone else does.
Why are they then as part of standard in allocator requirements in 20.1.5 ?


I think the original intention was that they were there to allow
pointer types that aren't T*. However, the standard says for
construct:

Effect: new ((void*)p) T(t)

I think that is a defect, since it requires that
allocator::pointer_type be castable to void*, which is not a good
requirement to have!
Is it an optional feature or are the compilers not being conformant ?


It's an optional feature whether or not the containers use it or not -
I don't see any mention of whether containers must call it or not,
which I think makes it optional.

Tom
Jul 22 '05 #13
"Alex Vinokur" <al****@big-foot.com> wrote in message news:<2n************@uni-berlin.de>...
"Sharad Kala" <no******************@yahoo.com> wrote in message news:2n***********@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n***********@uni-berlin.de...
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?


'allocate'only reserves raw memory. Wnen you insert objects in that
allocated memory, for example with vector 'push_back' method,
'construct' method must be invoked. It is invoked from vector
'push_back' method. Can you post vector 'push_back' method code from
the vector library file that you are using ?

Regards,
Jose Luis.
Jul 22 '05 #14

"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote in message
news:c2**************************@posting.google.c om...
"Alex Vinokur" <al****@big-foot.com> wrote in message news:<2n************@uni-berlin.de>...
"Sharad Kala" <no******************@yahoo.com> wrote in message news:2n***********@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2n***********@uni-berlin.de...
> 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?


'allocate'only reserves raw memory. Wnen you insert objects in that
allocated memory, for example with vector 'push_back' method,
'construct' method must be invoked. It is invoked from vector
'push_back' method. Can you post vector 'push_back' method code from
the vector library file that you are using ?

[snip]

--------------------------------------------
GNU g++ version 3.3.1 (cygming special).
GNU Standard C++ Library libstdc++-v3
--------------------------------------------

Here is the push_back() method from file stl_vector.h
http://www.mit.edu/afs/athena/astaff...s/stl_vector.h
--------- push_back() : BEGIN ---------
// [23.2.4.3] modifiers
/**
* @brief Add data to the end of the %vector.
* @param x Data to be added.
*
* This is a typical stack operation. The function creates an element at
* the end of the %vector and assigns the given data to it.
* Due to the nature of a %vector this operation can be done in constant
* time if the %vector has preallocated space available.
*/
void
push_back(const value_type& __x)
{
if (_M_finish != _M_end_of_storage)
{
_Construct(_M_finish, __x);
++_M_finish;
}
else
_M_insert_aux(end(), __x);
}
--------- push_back() : END -----------
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Jul 22 '05 #15

"Alex Vinokur" <al****@big-foot.com> wrote in message news:2n************@uni-berlin.de...
[snip] Here is the Nicolai M. Josuttis' code sample with cosmetic changes added by me to profile the executable:
http://groups.google.com/groups?thre...0uni-berlin.de

Here are log files for the following compilers
* GNU g++ : http://groups.google.com/groups?selm...0uni-berlin.de [snip]
We can see that
* GNU g++ 3.3.1 doesn't invoke construct() and destroy(); [snip]
"Matt Austern" <au*****@apple.com> wrote in message
http://article.gmane.org/gmane.comp....c++.devel/9815
On Jul 19, 2004, at 9:25 AM, pi******@dei.unipd.it wrote:
Hi everybody !

There's a behaviour in STL allocator I don't understand, I hope
somebody can
help me...

I'm trying to customize an allocator to track the insertion/deletion
of objects
in a container,
decorating the standard allocator functions construct() and destroy().

The problem is that it seems that those functions are never called in
STL
containers, instead
a global template function _Construct() (defined in <stl_construct.h>)
is
called, that is
completely unaware of allocators.


This is a bug.

[snip]

See also several relevant threads from news://news.gmane.org/gmane.comp.gcc.libstdc++.devel

The thread titled "Custom allocator for vector and libstdc++-v3"
http://thread.gmane.org/gmane.comp.g...++.devel/10087

The thread titled "allocator construct() / destruct() behaviour..."
http://thread.gmane.org/gmane.comp.g...c++.devel/9814

The thread titled "PATCH: use construct and destroy from user-provided allocators"
http://thread.gmane.org/gmane.comp.g...c++.devel/9847
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brian Genisio | last post by:
Hi all, I am developing some software, that creates a tree of information. For each node, currently I am overriding the new operator, because it is a requirement that after initialization, no...
3
by: Orjan Westin | last post by:
Hi, I have an interesting (read frustrating) problem. I'm writing a generic container class, which holds data as well as links to other instances of itself, like this: template<class T>...
13
by: John Harrison | last post by:
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type, or can you assume that the container will rebind the allocator to the...
2
by: Joshua Kolden | last post by:
STL allocators are templates so that when you write one you are obliged to make it work with any type. However, the Intel IPP library that we use has memory aligned allocators for each of 15...
15
by: natespamacct | last post by:
Hi All, I'm not sure if I'm dealing with a C++ question or a compiler question, so please forgive me if I'm asking in the wrong spot. If so, maybe someone can direct me to more appropriate spot....
3
by: Alex Vinokur | last post by:
Compiler GNU g++ version 3.4.4 (cygming special) Custom allocator for vector (see below) checks a return value of 'operator new'. If that value is NULL, the allocator "allocates" no memory....
4
by: sreedhar.cs | last post by:
Hi all, In my application,I want to place a vector in a specific location in shared memory.(a user supplied pointer). I understand that the STL allocator mechanism places the data objects within...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.