please explain why this is Undefined Behavior

V

Victor Bazarov

Ioannis said:
Victor said:
Examples are what they are, examples. It doesn't necessarily mean you
cannot use placement new with non-POD objects or for "copying".



Is this also guaranteed to work? SomeClass is a non-POD type here:


#include <iostream>
#include <new>

class SomeClass
{
public:
SomeClass() { std::cout<<"Constructor called!\n"; }
~SomeClass() { std::cout<<"Destructor called!\n"; }

void somefunc() const { std::cout<<"somefunc() called!\n"; }
};


int main()
{
using namespace std;

unsigned char *parray= new unsigned char[sizeof(SomeClass)];

SomeClass *pSomeClass= new(parray) SomeClass;

pSomeClass->somefunc();

pSomeClass->~SomeClass();

delete[] parray;
}


Of course it is! What could possibly make you think that it isn't?

V
 
R

REH

Ioannis Vranos said:
Victor said:
Examples are what they are, examples. It doesn't necessarily mean you
cannot use placement new with non-POD objects or for "copying".


Is this also guaranteed to work? SomeClass is a non-POD type here:


#include <iostream>
#include <new>

class SomeClass
{
public:
SomeClass() { std::cout<<"Constructor called!\n"; }
~SomeClass() { std::cout<<"Destructor called!\n"; }

void somefunc() const { std::cout<<"somefunc() called!\n"; }
};


int main()
{
using namespace std;

unsigned char *parray= new unsigned char[sizeof(SomeClass)];

SomeClass *pSomeClass= new(parray) SomeClass;

pSomeClass->somefunc();

pSomeClass->~SomeClass();

delete[] parray;
}

It's fine except you are calling the constructor twice.

REH
 
R

REH

REH said:
Ioannis Vranos said:
int main()
{
using namespace std;

unsigned char *parray= new unsigned char[sizeof(SomeClass)];

SomeClass *pSomeClass= new(parray) SomeClass;

pSomeClass->somefunc();

pSomeClass->~SomeClass();

delete[] parray;
}

It's fine except you are calling the constructor twice.

REH
Sorry, my bad. I misread "somefunc" as "SomeClass."
 
V

Victor Bazarov

REH said:
Sorry, my bad. I misread "somefunc" as "SomeClass."

Besides, there is no way to call a constructor, so it wouldn't compile
if it were written as pSomeClass->SomeClass();

V
 
I

Ioannis Vranos

Victor said:
Is this also guaranteed to work? SomeClass is a non-POD type here:


#include <iostream>
#include <new>

class SomeClass
{
public:
SomeClass() { std::cout<<"Constructor called!\n"; }
~SomeClass() { std::cout<<"Destructor called!\n"; }

void somefunc() const { std::cout<<"somefunc() called!\n"; }
};


int main()
{
using namespace std;

unsigned char *parray= new unsigned char[sizeof(SomeClass)];

SomeClass *pSomeClass= new(parray) SomeClass;

pSomeClass->somefunc();

pSomeClass->~SomeClass();

delete[] parray;
}



Of course it is! What could possibly make you think that it isn't?


I asked because the standard says that we can *only* read non-POD types as unsigned chars
(more accurately, the memcpy() back and forth retaining the original value is guaranteed
only for POD types).
 
V

Victor Bazarov

Ioannis said:
Victor said:
Is this also guaranteed to work? SomeClass is a non-POD type here:


#include <iostream>
#include <new>

class SomeClass
{
public:
SomeClass() { std::cout<<"Constructor called!\n"; }
~SomeClass() { std::cout<<"Destructor called!\n"; }

void somefunc() const { std::cout<<"somefunc() called!\n"; }
};


int main()
{
using namespace std;

unsigned char *parray= new unsigned char[sizeof(SomeClass)];

SomeClass *pSomeClass= new(parray) SomeClass;

pSomeClass->somefunc();

pSomeClass->~SomeClass();

delete[] parray;
}




Of course it is! What could possibly make you think that it isn't?



I asked because the standard says that we can *only* read non-POD types
as unsigned chars (more accurately, the memcpy() back and forth
retaining the original value is guaranteed only for POD types).

The difference about just reading the bytes into the memory of the object
and placement new is that placement new assures _construction_ whereas the
POD doesn't have any constructors to execute. You may of course construct
a POD object using placement new, but the standard allows you to do away
with it, but only for PODs.

V
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top