no response from allocator

S

Sepidar

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,MyAllocator> MyVector;

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.
 
B

Bo Persson

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,MyAllocator> MyVector;
::
:: 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
 
I

Ian Collins

Sepidar said:
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:

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?
 
S

Sepidar

<snip code>




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?

I'm using gcc 4.1.2...

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top