473,803 Members | 3,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

no response from allocator

hi all,
I'm trying to write a custom allocator for myself, using with
std::jvector. But it seems that I have no response from it. See the
code:

#include <iostream>
#include <vector>
#include <memory>

using std::cout;
using std::endl;

class MyClass
{
public:
MyClass(){cout< <"a new instance of MyClass created!"<<endl ;}
~MyClass(){cout <<"one instance of MyClass destroyd!"<<end l;}
};
typedef MyClass* MyClassPointer;

class MyAllocator:pub lic std::allocator< MyClassPointer>
{
public:
MyAllocator()
{
cout<<"message from MyAllocator::My Allocator"<<end l;
}
void destroy(pointer p)
{
cout<<"message from MyAllocator::de stroy"<<endl;
//whatever
//std::allocator< MyClassPointer> ::destroy(p);
}
void deallocate(poin ter p, size_type n)
{
cout<<"message from MyAllocator::de allocate"<<endl ;
//whatever
//std::allocator< MyClassPointer> ::deallocate(p, n);
}

};

typedef std::vector<MyC lassPointer,MyA llocatorMyVecto r;

int main()
{
MyVector vec;

vec.push_back(n ew MyClass());

vec.pop_back(); //free memory and print someting on screen
vec.clear(); //please do it!

return 0;
}

It is expected that the code return at least message from
MyAllocator's constructor, but there is nothing showing that. Please
help.

Sep 1 '07 #1
4 1385
Sepidar wrote:
:: hi all,
:: I'm trying to write a custom allocator for myself, using with
:: std::jvector. But it seems that I have no response from it. See the
:: code:
::
:: #include <iostream>
:: #include <vector>
:: #include <memory>
::
:: using std::cout;
:: using std::endl;
::
:: class MyClass
:: {
:: public:
:: MyClass(){cout< <"a new instance of MyClass created!"<<endl ;}
:: ~MyClass(){cout <<"one instance of MyClass destroyd!"<<end l;}
:: };
:: typedef MyClass* MyClassPointer;
::
:: class MyAllocator:pub lic std::allocator< MyClassPointer>
:: {
:: public:
:: MyAllocator()
:: {
:: cout<<"message from MyAllocator::My Allocator"<<end l;
:: }
:: void destroy(pointer p)
:: {
:: cout<<"message from MyAllocator::de stroy"<<endl;
:: //whatever
:: //std::allocator< MyClassPointer> ::destroy(p);
:: }
:: void deallocate(poin ter p, size_type n)
:: {
:: cout<<"message from MyAllocator::de allocate"<<endl ;
:: //whatever
:: //std::allocator< MyClassPointer> ::deallocate(p, n);
:: }
::
:: };
::
:: typedef std::vector<MyC lassPointer,MyA llocatorMyVecto r;
::
:: int main()
:: {
:: MyVector vec;
::
:: vec.push_back(n ew MyClass());
::
:: vec.pop_back(); //free memory and print someting on screen
:: vec.clear(); //please do it!
::
:: return 0;
:: }
::
:: It is expected that the code return at least message from
:: MyAllocator's constructor, but there is nothing showing that.
:: Please help.

I get this result:

message from MyAllocator::My Allocator
a new instance of MyClass created!
message from MyAllocator::de stroy
message from MyAllocator::de allocate
Seems ok to me.

Bo Persson
Sep 1 '07 #2
may i ask you what was your compiler/stl implementation?

Sep 2 '07 #3
Sepidar wrote:
hi all,
I'm trying to write a custom allocator for myself, using with
std::jvector. But it seems that I have no response from it. See the
code:
<snip code>
>
It is expected that the code return at least message from
MyAllocator's constructor, but there is nothing showing that. Please
help.
Looks OK to me, so I tried gcc and Sun CC, both gave

message from MyAllocator::My Allocator
a new instance of MyClass created!
message from MyAllocator::de allocate

You're not using some dodgy old compiler like VC6 are you?

--
Ian Collins.
Sep 2 '07 #4
On Sep 2, 3:28 am, Ian Collins <ian-n...@hotmail.co mwrote:
Sepidar wrote:
hi all,
I'm trying to write a custom allocator for myself, using with
std::jvector. But it seems that I have no response from it. See the
code:

<snip code>
It is expected that the code return at least message from
MyAllocator's constructor, but there is nothing showing that. Please
help.

Looks OK to me, so I tried gcc and Sun CC, both gave

message from MyAllocator::My Allocator
a new instance of MyClass created!
message from MyAllocator::de allocate

You're not using some dodgy old compiler like VC6 are you?

--
Ian Collins.
I'm using gcc 4.1.2...

Have you ever noticed that even your results are different from Bo?

Sep 2 '07 #5

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

Similar topics

5
2106
by: Scott Brady Drummonds | last post by:
Hi, everyone, A coworker and I have been pondering a memory allocation problem that we're having with a very large process. Our joint research has led us to the conclusion that we may have to replace the STL allocator. But, before I even attempt something like this, I wanted to ask some questions to this group: 1) We read online that STL caches allocated memory so that containers and their contents that are freed at one point in...
3
1880
by: Bernhard Kick | last post by:
Hi all, I saw this code in the book "Accelerated C++" (chapt 11, iirc): template <class T> class Vec { ... std::allocator<T> alloc; // object to handle memory allocation // ??would static member work here??: // static std::allocator<T> alloc;
3
1533
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> class node { public:
13
1952
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 correct type? For example I tried the following code which uses a 'wrong' allocator and was slightly surrised to find it compiles on the three compilers I tried it on #include <vector> #include <memory>
3
3257
by: Mark P | last post by:
Hi, I'm looking for some info on the default STL allocator, std::alloc. In particular, I'm wondering if it is optimized to handle many allocations of small objects. I'm thinking along the lines of a pool-based approach, for example. Any references would also be appreciated. Thanks,
7
3081
by: Grahamo | last post by:
Hi, can anybody tell me where I can get the boiler plate code for std::allocator. I need to have my version of new and delete called and want to get reference code. My compilers headers are all over the place with #ifdefs and what not, I'd like a clean copy. thanks much
6
13861
by: Juha Nieminen | last post by:
I tested the speed of a simple program like this: //------------------------------------------------------------ #include <list> #include <boost/pool/pool_alloc.hpp> int main() { typedef std::list<intList_t; // default allocator //typedef std::list<int, boost::pool_allocator<int List_t;
2
2480
by: * Tong * | last post by:
Hi, I'm following the example in "C++ Templates: The Complete Guide", section 5.4 Template Template Parameters. It's basics/stack8.hpp example has a statement of "#include <allocator>", but I got an error for it. $ cat -n allocator.cc 1 #include <allocator>
16
2706
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 allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6842
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.