Writing an allocator

P

Protoman

Why won't the compiler let me write:

#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;

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

int main()
{
Allocator<long long> alloc;
long long* i=alloc.allocate();
system("PAUSE");
return 0;
}

Any suggestions? Thanks.
 
N

Neelesh Bodas

Protoman said:
Why won't the compiler let me write:

#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;

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


the argument to placement new is a memory location (void*) . long long
int cannot be converted to void*. Hence this will not compile. Use
&pool instead.
private:
static T pool[1000000];
};
You need to define the static memeber "pool" outside the class:
int main()
{
Allocator<long long> alloc;
long long* i=alloc.allocate();
system("PAUSE");
return 0;
}

Any suggestions? Thanks.

With these two changes it worked in my case.
 
O

Old Wolf

Protoman said:
Why won't the compiler let me write:

#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;

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


C++ does not have "long long".
 

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,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top