473,406 Members | 2,710 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,406 software developers and data experts.

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!"<<endl;}
};
typedef MyClass* MyClassPointer;

class MyAllocator:public std::allocator<MyClassPointer>
{
public:
MyAllocator()
{
cout<<"message from MyAllocator::MyAllocator"<<endl;
}
void destroy(pointer p)
{
cout<<"message from MyAllocator::destroy"<<endl;
//whatever
//std::allocator<MyClassPointer>::destroy(p);
}
void deallocate(pointer p, size_type n)
{
cout<<"message from MyAllocator::deallocate"<<endl;
//whatever
//std::allocator<MyClassPointer>::deallocate(p,n);
}

};

typedef std::vector<MyClassPointer,MyAllocatorMyVector;

int main()
{
MyVector vec;

vec.push_back(new 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 1368
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!"<<endl;}
:: };
:: typedef MyClass* MyClassPointer;
::
:: class MyAllocator:public std::allocator<MyClassPointer>
:: {
:: public:
:: MyAllocator()
:: {
:: cout<<"message from MyAllocator::MyAllocator"<<endl;
:: }
:: void destroy(pointer p)
:: {
:: cout<<"message from MyAllocator::destroy"<<endl;
:: //whatever
:: //std::allocator<MyClassPointer>::destroy(p);
:: }
:: void deallocate(pointer p, size_type n)
:: {
:: cout<<"message from MyAllocator::deallocate"<<endl;
:: //whatever
:: //std::allocator<MyClassPointer>::deallocate(p,n);
:: }
::
:: };
::
:: typedef std::vector<MyClassPointer,MyAllocatorMyVector;
::
:: int main()
:: {
:: MyVector vec;
::
:: vec.push_back(new 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::MyAllocator
a new instance of MyClass created!
message from MyAllocator::destroy
message from MyAllocator::deallocate
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::MyAllocator
a new instance of MyClass created!
message from MyAllocator::deallocate

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.comwrote:
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::MyAllocator
a new instance of MyClass created!
message from MyAllocator::deallocate

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
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...
3
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...
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...
3
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...
7
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...
6
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...
2
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.