Previous, I have the following code.
double* a[100];
for (int i = 0; i < 100; i++) {
// Initialize.
a[i] = 0;
}
The purpose of initialize array of a to 0 is that, when I iterative delete element of a, everything will work fine still even there are no memory allocated still for element of a.
for (int i = 0; i < 100; i++) {
// Fine.
delete a[i];
}
Now, I would like to take advantage of auto_ptr, to avoid having manual call to delete.
std::auto_ptr<double> a[100];
for (int i = 0; i < 100; i++) {
// Initialize. Is there any need for me to do so still?
a[i] = std::auto_ptr<double>(0);
}
I was wondering, whether there is any need for me to initialize auto_ptr to hold a null pointer? My feeling is no. I just want to confirm on this, so that there aren't any catch behind.
The C++03 specifies the constructor of auto_ptr as follows:
explicit auto_ptr(X* p =0) throw(); // Note the default argument
Postconditions: *this holds the pointer p.
This means that the below is perfectly well-formed. There is no need to initialize
auto_ptr<int> a = auto_ptr<int>();
std::auto_ptr's default constructor does the NULL-assignment for you -- or, as the standard (ISO/IEC 14882:1998) puts it, the constructor is declared as:
explicit auto_ptr(X* p =0) throw();
(X being the template parameter class, i.e., this is for std::auto_ptr<X>).
You can zero initialize all members of an array using:
double* a[100] = {0}; // is equivalent
An alternative to deletion by using for_each:
struct delete_object
{
template <typename T>
void operator()(T *ptr){ delete ptr;}
};
//later in the code...
std::for_each( &a[ 0 ], &a[ 0 ] + sizeof a / sizeof a[ 0 ], delete_object());
Now for your question:
whether there is any need for me to initialize auto_ptr to hold a null pointer?
There is no need to initialize the auto_ptrs array. The members will be default initialized if you leave it out.
However, note that auto_ptr may not be usable for its move semantics (copy of ownership) if you need to pass around the pointers to other functions. Also, in the coming standard auto_ptr is likely to be deprecated. Try using something like std::tr1::unique_ptr or std::tr1::shared_ptr (the latter is a reference counted smart pointer).
Related
So, for instance, I have the following code which I want a object's pointer
member to point to a memory which was pointed by another temporary object's
member.
struct A {
int * vals;
A(): vals(new int[10]) { }
~A(){ delete[] vals; }
};
int main() {
A a;
{
A temp;
for (int i = 0; i < 10; ++i) {
temp.vals[i] = 100;
}
a.vals = temp.vals;
temp.vals = nullptr; // avoid double free
}
I set temp.vals to nullptr in case the destructor of temp will free that
memory. So far so good, I guess. However, if I change the vals to a dynamic
array, i.e. a pointer to pointers:
struct A {
int ** vals;
A(): vals(new int*[10]) {
for (int i = 0; i < 10; ++i) {
vals[i] = new int;
}
}
~A(){
for (int i = 0; i < 10; ++i) {
delete vals[i]; // illegal to dereference nullptr
}
delete [] vals;
}
};
int main() {
A a;
{
A temp;
for (int i = 0; i < 10; ++i) {
temp.vals[i] = new int(1);
}
a.vals = temp.vals;
temp.vals = nullptr; // avoid double free
}
}
I have add a for loop in destructor to handle the nested allocated memory, and
to avoid the memory be freed by the destructor of temp, I set temp.vals to
nullptr, which, however will cause a segmentation fault since when destructor
of temp is called, it is illegal to dereference a nullptr.
So my question is, how to correct set the destructor to handle the dynamic array.
I'm not a native speaker, so please forgive my grammar mistakes.
The typical C++ solution looks a bit different:
class A {
private:
int* vals;
public:
A(): vals(new int[10]) { }
~A(){ delete[] vals; }
A (A const& src); // Copy constructor
A (A&& src) : vals (src.vals) { src.vals = nullptr; }
A& operator=(A const&); // Assignment
A& operator=(A &&);
};
You can now write a = std::move(temp). Outside the class, you don't need to know how the inside works.
For your 2D array, just define the same special member functions. This is usually called the "Rule of Five". If you need a destructor, you probably need the other 4 functions as well. The alternative is the "Rule of Zero". Use std::vector or another class that manages memory for you.
However, if I change the vals to a dynamic array, i.e. a pointer to pointers
In the first program, vals is a pointer. It points to a dynamic array of integers. In the second program, vals is also a pointer. It points to a dynamic array of pointers.
how to correct set the destructor to handle the dynamic array.
You set vals to null. If null is a valid state for vals, then it isn't correct to unconditionally indirect through it in the destructor. You can use a conditional statement to do so only when vals isn't null.
However, the program is hardly safe because vals is public, and thus it is easy to mistakenly write a program where it is assigned to point to memory that isn't owned by the object. In cases where destructor cleans up an owned resource, it is important to encapsulate the resource using private access to prevent accidental violation of class invariants that are necessary to correctly handle the resource.
Now that vals is no longer outside of member functions, you cannot transfer the ownership like you did in your example. The correct way to transfer the ownership is to use move assignment (or constructor). The implicitly generated move constructor cannot handle an owning bare pointer correctly. You must implement them, as well as the copy assignment and constructor.
Furthermore, you should use an owning bare pointer in the first place. You should use a smart pointer instead. If you simply replaced the bare pointers with unique pointer, then the implicitly generated move assignment and constructor would handle the resource correctly, and the copy assignment and constructor would be implicitly deleted.
Lastly, the standard library has a container for dynamic arrays. Its called std::vector. There's typically no need to attempt to re-implement it. So in conclusion, I recommend following:
std::vector<int> a;
{
std::vector<int> temp;
for (int i = 0; i < 10; ++i) {
temp.vals[i] = 1;
}
a = std::move(temp);
}
There are still issues such as the temporary variable being entirely unnecessary, and the loop could be replaced with a standard algorithm, but I tried to keep it close to the original for the sake of comparison.
P.S. It's pretty much never useful to dynamically allocate individual integers.
If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class
class Foo{
public:
Foo(int i) {}
}
How to declare an array or a vector which contains 10000 Foo objects?
For an array you would have to provide an initializer for each element of the array at the point where you define the array.
For a vector you can provide an instance to copy for each member of the vector.
e.g.
std::vector<Foo> thousand_foos(1000, Foo(42));
Actually, you can do it as long you use an initialization list, like
Foo foos[4] = { Foo(0),Foo(1),Foo(2),Foo(3) };
however with 10000 objects this is absolutely impractical. I'm not even sure if you were crazy enough to try if the compiler would accept an initialization list this big.
sbi had the best answer for plain arrays, but didn't give an example. So...
You should use placement new:
char *place = new char [sizeof(Foo) * 10000];
Foo *fooArray = reinterpret_cast<Foo *>(place);
for (unsigned int i = 0; i < 10000; ++i) {
new (fooArray + i) Foo(i); // Call non-default constructor
}
Keep in mind that when using placement new, you are responsible for calling the objects' destructors -- the compiler won't do it for you:
// In some cleanup code somewhere ...
for (unsigned int i = 0; i < 10000; ++i) {
fooArray[i].~Foo();
}
// Don't forget to delete the "place"
delete [] reinterpret_cast<char *>(fooArray);
This is about the only time you ever see a legitimate explicit call to a destructor.
NOTE: The first version of this had a subtle bug when deleting the "place". It's important to cast the "place" back to the same type that was newed. In other words, fooArray must be cast back to char * when deleting it. See the comments below for an explanation.
You'd need to do an array of pointers to Foo.
Foo* myArray[10000];
for (int i = 0; i < 10000; ++i)
myArray[i] = new Foo(i);
When you declare an object that has no default constructor, you must initialize it in the declaration.
Foo a; // not allowed
Foo b(0); // OK
The same goes for arrays of such types:
Foo c[2]; // not allowed
Foo d[2] = { 0, 1 }; // OK
Foo e[] = { Foo(0), Foo(1), Foo(2) }; // also OK
In your case, you'll probably find it impractical to initialize all 10,000 elements like that, so you might wish to rethink whether the class really shouldn't have a default constructor.
The only way to define an array of a class with no default constructor would be to initialize it right away - not really an option with 10000 objects.
You can, however, allocate enough raw memory whichever way you want and use placement new to create the objects in that memory. But if you want to do this, it's better to use std::vector which does exactly that:
#include <iostream>
#include <vector>
struct foo {
foo(int) {}
};
int main()
{
std::vector<foo> v;
v.resize(10000,foo(42));
std::cout << v.size() '\n';
return 0;
}
You have to use the aggregate initializer, with 10000 of inidividual initializers between the {}
Foo array[10000] = { 1, 2, 3, ..., 10000 };
Of course, specifying 10000 initializers is something from the realm of impossible, but you asked for it yourself. You wanted to declare an array of 10000 objects with no default constructor.
class single
{
int data;
public:
single()
{
data = 0;
}
single(int i)
{
data = i;
}
};
// in main()
single* obj[10000];
for (unsigned int z = 0; z < 10000; z++)
{
obj[z] = new single(10);
}
Another option might be to use an array of boost::optional<Foo>:
boost::optional<Foo> foos[10]; // No construction takes place
// (similar to vector::reserve)
foos[i] = Foo(3); // Actual construction
One caveat is that you'll have to access the elements with pointer syntax:
bar(*foos[2]); // "bar" is a function taking a "Foo"
std::cout << foos[3]->baz(); // "baz" is a member of "Foo"
You must also be careful not to access an unitialized element.
Another caveat is that this not a true replacement for an array of Foo, as you won't be able to pass it to a function that expects the latter.
In straight C, using int foo[10000] = {1}; will initialize the first array item to 1 and the remainder of the array to zero. Does C++ not auto-initialize unspecified array members, or does this require a default constructor?
If it makes sense for your class, you could provide a default value for your constructor parameter:
class Foo
{
public:
explicit Foo(int i = 0);
}
Now you have a default constructor. (A "default constructor" is a constructor that can be called with no arguments: FAQ)
I'd also recommend making your constructor explicit, as I did above. It will prevent you from getting Foos from ints when you don't want request them.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor
If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class
class Foo{
public:
Foo(int i) {}
}
How to declare an array or a vector which contains 10000 Foo objects?
For an array you would have to provide an initializer for each element of the array at the point where you define the array.
For a vector you can provide an instance to copy for each member of the vector.
e.g.
std::vector<Foo> thousand_foos(1000, Foo(42));
Actually, you can do it as long you use an initialization list, like
Foo foos[4] = { Foo(0),Foo(1),Foo(2),Foo(3) };
however with 10000 objects this is absolutely impractical. I'm not even sure if you were crazy enough to try if the compiler would accept an initialization list this big.
sbi had the best answer for plain arrays, but didn't give an example. So...
You should use placement new:
char *place = new char [sizeof(Foo) * 10000];
Foo *fooArray = reinterpret_cast<Foo *>(place);
for (unsigned int i = 0; i < 10000; ++i) {
new (fooArray + i) Foo(i); // Call non-default constructor
}
Keep in mind that when using placement new, you are responsible for calling the objects' destructors -- the compiler won't do it for you:
// In some cleanup code somewhere ...
for (unsigned int i = 0; i < 10000; ++i) {
fooArray[i].~Foo();
}
// Don't forget to delete the "place"
delete [] reinterpret_cast<char *>(fooArray);
This is about the only time you ever see a legitimate explicit call to a destructor.
NOTE: The first version of this had a subtle bug when deleting the "place". It's important to cast the "place" back to the same type that was newed. In other words, fooArray must be cast back to char * when deleting it. See the comments below for an explanation.
You'd need to do an array of pointers to Foo.
Foo* myArray[10000];
for (int i = 0; i < 10000; ++i)
myArray[i] = new Foo(i);
When you declare an object that has no default constructor, you must initialize it in the declaration.
Foo a; // not allowed
Foo b(0); // OK
The same goes for arrays of such types:
Foo c[2]; // not allowed
Foo d[2] = { 0, 1 }; // OK
Foo e[] = { Foo(0), Foo(1), Foo(2) }; // also OK
In your case, you'll probably find it impractical to initialize all 10,000 elements like that, so you might wish to rethink whether the class really shouldn't have a default constructor.
The only way to define an array of a class with no default constructor would be to initialize it right away - not really an option with 10000 objects.
You can, however, allocate enough raw memory whichever way you want and use placement new to create the objects in that memory. But if you want to do this, it's better to use std::vector which does exactly that:
#include <iostream>
#include <vector>
struct foo {
foo(int) {}
};
int main()
{
std::vector<foo> v;
v.resize(10000,foo(42));
std::cout << v.size() '\n';
return 0;
}
You have to use the aggregate initializer, with 10000 of inidividual initializers between the {}
Foo array[10000] = { 1, 2, 3, ..., 10000 };
Of course, specifying 10000 initializers is something from the realm of impossible, but you asked for it yourself. You wanted to declare an array of 10000 objects with no default constructor.
class single
{
int data;
public:
single()
{
data = 0;
}
single(int i)
{
data = i;
}
};
// in main()
single* obj[10000];
for (unsigned int z = 0; z < 10000; z++)
{
obj[z] = new single(10);
}
Another option might be to use an array of boost::optional<Foo>:
boost::optional<Foo> foos[10]; // No construction takes place
// (similar to vector::reserve)
foos[i] = Foo(3); // Actual construction
One caveat is that you'll have to access the elements with pointer syntax:
bar(*foos[2]); // "bar" is a function taking a "Foo"
std::cout << foos[3]->baz(); // "baz" is a member of "Foo"
You must also be careful not to access an unitialized element.
Another caveat is that this not a true replacement for an array of Foo, as you won't be able to pass it to a function that expects the latter.
In straight C, using int foo[10000] = {1}; will initialize the first array item to 1 and the remainder of the array to zero. Does C++ not auto-initialize unspecified array members, or does this require a default constructor?
If it makes sense for your class, you could provide a default value for your constructor parameter:
class Foo
{
public:
explicit Foo(int i = 0);
}
Now you have a default constructor. (A "default constructor" is a constructor that can be called with no arguments: FAQ)
I'd also recommend making your constructor explicit, as I did above. It will prevent you from getting Foos from ints when you don't want request them.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor
I tried to initiallize my own int array's class but somthing in the operator overloading isnt working, for somereason my object's d'tor is being called before I want it to and it makes my code crash:
#include "stdafx.h"
#include "Array.h"
int _tmain(int argc, _TCHAR* argv[])
{
Array arr(6);
arr[3] = 6; //code crashes here
printf("%d\n", arr[0]);
return 0;
}
and now the class:
header file:
#pragma once
#include <malloc.h>
class Array
{
public:
//ctor dtor and operator overloading
Array(int);
Array(int *, int);
~Array();
Array operator[](int i);
//here memebers are defined
int *arr;
int size;
};
----cpp file---
#include "stdafx.h"
#include "Array.h"
//the function mallocs size for the array.
Array::Array(int g = 1) :size(g) //diplomatic value for size =1
{
this->arr = (int*)malloc(sizeof(int) * this->size);
}
Array::Array(int *p, int m_size=1) :arr(p), size(m_size)
{}
Array::~Array()
{
delete arr;
}
after this function my object is being deleted by the d'tor
Array Array::operator[] (int i){
for (int j=0; j < i; ++j)
{
++(this->arr);
}
return *this;
}
Because the operator[] function returns an Array object by value. That means when you do your assignment arr[3] = 6 the operator returns a copy of this, the compiler then implicitly creates an array object from the integer 6 (because it has a matching constructor) and suddenly your assignment looks like
arr[3] = Array(6);
Somewhere the temporary array objects (the one returned by the operator[] function, and the one the compiler creates on the right-hand side of the assignment) have to be destructed, but by then you have multiple object all using the same pointer arr inside themselves. The first temporary object is destructed and it deletes the pointer, leaving all other objects with an invalid pointer. Then the next temporary object is destructed and tries to delete the now invalid pointer. Boom, you have a crash.
There are two things you need to do to fix the problem. The first is to realize that the operator[] function returns the wrong type, it should return a reference to an element of the contained array instead:
int& Array::operator[](size_t const i)
{
return arr[i];
}
The second thing you need to do is to read about the rules of three, five and zero.
Also, to prevent the compiler from mistakenly construct an object from an int value, you should mark the constructor as explicit:
explicit Array(int);
The main problem
Regardless of other problems in your code, the signature of your operator[] is wrong:
Array Array::operator[] (int i){
...
}
You don't return an element of the array here ! You return a whole new array instead. So in your faulty statement, a new temporary Array gets constructed and gets destructed at the end of the enclosing expression.
Try this instead. At least it will not crash immediately:
int& Array::operator[] (int i){
return arr[i];
}
The cause of the crash
Unfortunately, you allocate arr using malloc(). Never do this in C++ but use new[] instead. This would not lead to a crash by itself. But in addition in your destructor you use delete.
It's either malloc()/free() or new/delete or new[]/delete[] Any other combination is undefined behavior (this causes the crash in your original code, when the temporary Array got destroyed).
Correct the problem as follows:
Array::Array(int g = 1) :size(g) //diplomatic value for size =1
{
arr = new int[size];
}
Array::~Array()
{
delete[] arr;
}
It will compile and work: online demo
Other serious issue
Finally to make the things even worse, your original indexing operator returns *this, making so a clone of your existing Array. This is now solved. But if you'd make any copy of your Array you'd have another problem:
This will cause the arr pointer to be used in two places. The first Array that gests destructed will invalidate the arr pointer that is used in the original Array. Any subsequent dereferencing of its arr would be undefined behavior.
It's not solved in my online demo, so I I'd strongly suggest to read about the rule of 3
If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class
class Foo{
public:
Foo(int i) {}
}
How to declare an array or a vector which contains 10000 Foo objects?
For an array you would have to provide an initializer for each element of the array at the point where you define the array.
For a vector you can provide an instance to copy for each member of the vector.
e.g.
std::vector<Foo> thousand_foos(1000, Foo(42));
Actually, you can do it as long you use an initialization list, like
Foo foos[4] = { Foo(0),Foo(1),Foo(2),Foo(3) };
however with 10000 objects this is absolutely impractical. I'm not even sure if you were crazy enough to try if the compiler would accept an initialization list this big.
sbi had the best answer for plain arrays, but didn't give an example. So...
You should use placement new:
char *place = new char [sizeof(Foo) * 10000];
Foo *fooArray = reinterpret_cast<Foo *>(place);
for (unsigned int i = 0; i < 10000; ++i) {
new (fooArray + i) Foo(i); // Call non-default constructor
}
Keep in mind that when using placement new, you are responsible for calling the objects' destructors -- the compiler won't do it for you:
// In some cleanup code somewhere ...
for (unsigned int i = 0; i < 10000; ++i) {
fooArray[i].~Foo();
}
// Don't forget to delete the "place"
delete [] reinterpret_cast<char *>(fooArray);
This is about the only time you ever see a legitimate explicit call to a destructor.
NOTE: The first version of this had a subtle bug when deleting the "place". It's important to cast the "place" back to the same type that was newed. In other words, fooArray must be cast back to char * when deleting it. See the comments below for an explanation.
You'd need to do an array of pointers to Foo.
Foo* myArray[10000];
for (int i = 0; i < 10000; ++i)
myArray[i] = new Foo(i);
When you declare an object that has no default constructor, you must initialize it in the declaration.
Foo a; // not allowed
Foo b(0); // OK
The same goes for arrays of such types:
Foo c[2]; // not allowed
Foo d[2] = { 0, 1 }; // OK
Foo e[] = { Foo(0), Foo(1), Foo(2) }; // also OK
In your case, you'll probably find it impractical to initialize all 10,000 elements like that, so you might wish to rethink whether the class really shouldn't have a default constructor.
The only way to define an array of a class with no default constructor would be to initialize it right away - not really an option with 10000 objects.
You can, however, allocate enough raw memory whichever way you want and use placement new to create the objects in that memory. But if you want to do this, it's better to use std::vector which does exactly that:
#include <iostream>
#include <vector>
struct foo {
foo(int) {}
};
int main()
{
std::vector<foo> v;
v.resize(10000,foo(42));
std::cout << v.size() '\n';
return 0;
}
You have to use the aggregate initializer, with 10000 of inidividual initializers between the {}
Foo array[10000] = { 1, 2, 3, ..., 10000 };
Of course, specifying 10000 initializers is something from the realm of impossible, but you asked for it yourself. You wanted to declare an array of 10000 objects with no default constructor.
class single
{
int data;
public:
single()
{
data = 0;
}
single(int i)
{
data = i;
}
};
// in main()
single* obj[10000];
for (unsigned int z = 0; z < 10000; z++)
{
obj[z] = new single(10);
}
Another option might be to use an array of boost::optional<Foo>:
boost::optional<Foo> foos[10]; // No construction takes place
// (similar to vector::reserve)
foos[i] = Foo(3); // Actual construction
One caveat is that you'll have to access the elements with pointer syntax:
bar(*foos[2]); // "bar" is a function taking a "Foo"
std::cout << foos[3]->baz(); // "baz" is a member of "Foo"
You must also be careful not to access an unitialized element.
Another caveat is that this not a true replacement for an array of Foo, as you won't be able to pass it to a function that expects the latter.
In straight C, using int foo[10000] = {1}; will initialize the first array item to 1 and the remainder of the array to zero. Does C++ not auto-initialize unspecified array members, or does this require a default constructor?
If it makes sense for your class, you could provide a default value for your constructor parameter:
class Foo
{
public:
explicit Foo(int i = 0);
}
Now you have a default constructor. (A "default constructor" is a constructor that can be called with no arguments: FAQ)
I'd also recommend making your constructor explicit, as I did above. It will prevent you from getting Foos from ints when you don't want request them.
Try this.
Foo **ppInstances=0;
size_t total_instances = 10000;
for(int parent=0;parent < total_instances;parent++){
ppInstances[parent]=new Foo( parent );
ppInstances++;
}
for(int parent=0;parent < total_instances;parent++){
delete *ppInstances;
ppInstances--;
}
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template<typename T>
using storage_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Foo;
size_t i = 55;
storage_t<Foo> items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast<Foo*>(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast<Foo*>(items + i) = Foo(27); // assign Foo
reintrepret_cast<Foo*>(items + i)->~Foo() // call destructor