473,414 Members | 1,703 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.

Writing an allocator

Why won't the compiler let me write this:

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

It keeps saying: "expected ; at end of input". Can you help me fix
this? Thanks.

Nov 22 '05 #1
3 2755
Protoman wrote:
Why won't the compiler let me write this:

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

It keeps saying: "expected ; at end of input". Can you help me fix
this? Thanks.


The previous two problems. Plus the missing semicolon at the end of the
structure.

Nov 22 '05 #2

Neelesh Bodas wrote:
Protoman wrote:
Why won't the compiler let me write this:

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

But why doesn't this work:

template <class T, long long X>
class Allocator
{
public:
Allocator(){}
T* allocate(){static long long i=0; T* j=new(&pool[i]) T; i++; return
&*j;}
private:
static T pool[X];
};
template<class T,long long X> T Allocator<T>::pool[X];
int main()
{
Allocator<long long,100> alloc;
long long* i=alloc.allocate();
system("PAUSE");
return 0;
}

Any suggestions? Thanks. It keeps saying: "expected ; at end of input". Can you help me fix
this? Thanks.


The previous two problems. Plus the missing semicolon at the end of the
structure.


Nov 22 '05 #3

Protoman wrote:

But why doesn't this work:

template <class T, long long X>
class Allocator
{
public:
Allocator(){}
T* allocate(){static long long i=0; T* j=new(&pool[i]) T; i++; return
&*j;}
To use placement new, use #include <new>

private:
static T pool[X];
};
template<class T,long long X> T Allocator<T>::pool[X];
should be Allocator<T,X> and not just Allocator<T>
int main()
{
Allocator<long long,100> alloc;
long long* i=alloc.allocate();
system("PAUSE");
return 0;
}


Hope this helps.

Nov 22 '05 #4

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...
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: 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...
3
by: Protoman | last post by:
Why won't the compiler let me write: #include <iostream> #include <cstdlib> #include <new> using namespace std; template <class T> class Allocator {
7
by: mavrik | last post by:
Hi, I've written allocator for STL vectors, which take all memory from my heap. But I don't have any idea to implement the same for Maps. As maps take Key and Values. So should I use the same...
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: 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?
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
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
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
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.