How to create a C-style array without calling default constructors?
I am writing a memory-managing template class in which I want to create a C-style array of fixed size, to serve as a heap. I keep the objects stored in an array like this:
T v[SIZE];
As this only serves the role as a heap开发者_如何学JAVA that can hold T objects, I don't want the T default constructor to get automatically called for every object in the array.
I thought about the solution to define the heap like this:
char v[SIZE * sizeof(T)];
...but this will give me alignment problems.
Is there any better way to achieve this?
ADD: As I have special run time requirements, it is essential that this class doesn't do any allocations on the global heap.
ADD 2: SIZE is a template argument and known at compile-time.
The standard containers use allocators to seperate allocation/deallocation from construction/destruction. The standard library supplies a single allocator which allocates on the heap.
This code declares an array big enough to hold SIZE
elements of type T
with the correct allignment:
typedef typename std::tr1::aligned_storage<sizeof(T),std::tr1::alignment_of<T>::value>::type aligned_storage;
aligned_storage array[SIZE];
The solution using std::allocator
can't be used to declare an array on the stack, and as the standard containers require that custom allocators hold no state, a custom allocator can't be portably used to allocate on the stack either.
If your compiler doesn't support std::tr1::alignment_of
you can use boost::alignment_of
instead.
What you are looking for is called an Allocator. A good overview can be found here: http://www.codeproject.com/KB/cpp/allocator.aspx
What I'd probably do is create an array of char (about like you've already considered), but allocate enough space for one more object than you really need. You'll then need to write a bit of code to find the correct alignment for your objects in that space.
The largest alignment requirement for an object is its own size (otherwise arrays of those objects couldn't be contiguous, which is required). Therefore, you pick the first address in the char-buffer that's a multiple of sizeof(T), and start your array from there.
Weird, but should work:
long long v[size * sizeof(T)/sizeof(long long)+1];
This buffer will be alligned on 64 bits.
But better will be allocate memory by new
.
In any case, if size
is variable - compiller will allocate memory dynamically through malloc/new (and not on stack).
EDIT: You can't use std::auto_ptr
to automaticaly free buffer. Instead scoped_arr
from boost can be used
You can use a struct to handle the alignment issue.
struct Tbuffer { char data_[ sizeof( T ) ]; }
struct Tbuffer heap[ SIZE ];
How I probably will do it (after looking on EASTL fixed_vector implementation):
PRAGMA_PRE_ALIGN(sizeof(T)) char[SIZE * sizeof(T)]; PRAGMA_POST_ALIGN(sizeof(T))
...and implement compiler specific PRAGMA_PRE_ALIGN and PRAGMA_POST_ALIGN macros, that insert the correct #pragmas.
Unfortunately, boost and tr1 are not possible for this project.
精彩评论