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

Is this a good method of repersenting an allocator?

Is this a good method of repersenting an allocator?:

template <class T, long long X>
class Allocator
{
public:
Allocator(){}
T* allocate()
{
static long long i=0;
T* j=new(&pool[i]) T;
i++;
if(i>X)throw bad_alloc();
else return &*j;
}
private:
static T pool[X];
};
template<class T,long long X> T Allocator<T,X>::pool[X];

Any suggestions? Thanks!!!

Nov 22 '05 #1
2 1265
Protoman wrote:
Is this a good method of repersenting an allocator?:

template <class T, long long X>
class Allocator
{
public:
Allocator(){}
T* allocate()
{
static long long i=0;
T* j=new(&pool[i]) T;
i++;
if(i>X)throw bad_alloc();
else return &*j;
}
private:
static T pool[X];
};
template<class T,long long X> T Allocator<T,X>::pool[X];

Any suggestions? Thanks!!!


There's a couple of issues I have.

1) An allocator is meant to allocate bytes not objects. The memory
returned by your allocator is a fully constructed object which will then
be constructed again when when the memory is returned to the program. So
you will end up with doubly constructed objects. Instead you should do
something like this

class Allocator
{
public:
T* allocate()
{
static long long i=0;
T* j = (T*)&pool[i*sizeof(T)]
i++;
return j;
}
private:
static char pool[X*sizeof(T)];
};

Because pool is an array of char, there is construction of objects
happening.

2) You need to implement a lot more members and types for your code to
be considered a complete allocator.

3) I didn't understand the purpose of

else return &*j;

why not

else return j;

john
Nov 22 '05 #2
> Because pool is an array of char, there is construction of objects
happening.


Typo

Because pool is an array of char, there is *no* construction of objects
happening.

john
Nov 22 '05 #3

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: 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...
7
by: bluekite2000 | last post by:
new/delete, std::allocator, pool_allocator or mt_allocator??? What r the pros and cons if each method? PS: I m designing a matrix/tensor/vector lib, all of which call base_constructor of class...
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...
10
by: Atlas | last post by:
To swap to nodes in list, changing pointers is enough. But the stl swap method seems to copy their values, doesn't it?
48
by: Tony | last post by:
How much bloat does the STL produce? Is it a good design wrt code bloat? Do implementations vary much? Tony
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.