Does this code have undefined behavior in C++?
#include <cstdlib>
int main() {
int *ip = static_cast<int *>(std::malloc(sizeof *ip));
*ip = 42; //is this accessing an object that has not started its lifetime?
free(ip);
}
Notes:
std::malloc
has the semantics that it has in C. In C std::malloc
creates valid int
s, so it should be valid?
The lifetime of an object or reference is a runtime property of the object or reference. An object is said to have non-vacuous initialization if it is of a class or aggregate type and it or one of its subobjects is initialized by a constructor other than a trivial default constructor. [ Note: Initialization by a trivial copy/move constructor is non-vacuous initialization. — end note ] The lifetime of an object of type T begins when:
(1.1) storage with the proper alignment and size for type T is obtained, and
(1.2) if the object has non-vacuous initialization, its initialization is complete,except that if the object is a union member or subobject thereof ...
I'm fairly sure this quote answers my question but I don't understand it well enough to tell if it is saying yes or no.