How to create a C-style array without calling default constructors?

I am writing a memory management template class in which I want to create a C-style fixed-size array to serve as a heap. I save the objects in an array as follows:

T v[SIZE];

Since this only serves as a heap role that can hold T objects, I don’t want the default constructor T to be automatically called for every object in the array.

I thought of a decision to define a bunch like this:

char v[SIZE * sizeof(T)];

... but it will give me alignment problems.

Is there a better way to achieve this?

ADD: Since I have specific runtime requirements, it is important that this class does not make any allocations on the global heap.

ADD 2: SIZE is a template argument and is known at compile time.

+5
6

/ /. , .

, , SIZE T :

typedef typename std::tr1::aligned_storage<sizeof(T),std::tr1::alignment_of<T>::value>::type aligned_storage;
aligned_storage array[SIZE];

, std::allocator, , , , .

std::tr1::alignment_of, boost::alignment_of.

+6

, :

long long v[size * sizeof(T)/sizeof(long long)+1];

64 . new.

, size - compiller malloc/new ( ).

EDIT: std::auto_ptr . scoped_arr boost

0

, , , , char ( ), , . , .

- ( , ). char -buffer, sizeof (T), .

0

struct .

struct Tbuffer { char data_[ sizeof( T ) ]; }

struct Tbuffer heap[ SIZE ];
0

, , ( EASTL fixed_vector):

PRAGMA_PRE_ALIGN(sizeof(T)) char[SIZE * sizeof(T)]; PRAGMA_POST_ALIGN(sizeof(T))

... and implement compiler-specific macros PRAGMA_PRE_ALIGN and PRAGMA_POST_ALIGN that insert the correct #pragmas.

Unfortunately, boost and tr1 are not possible for this project.

0
source

All Articles