Is this code undefined behaviour in C++?
...
float *f = (float*)malloc(sizeof(float) * 3);
if (f == NULL)
{
// handle error
}
f[0] = 1.0;
f[1] = 2.0;
f[2] = 3.0;
...
// code using f[0] to f[2] ...
...
IMO this is legal for POD types (like float, double, int etc.).
I know it's ugly and that it shouldn't be used.
malloc(and any similar functions) forever, unless it is required for placement-new.void* buffer = operator new(64). Very useful if you want to use C++ idioms with raw buffers or placement new. It also supportsstd::nothrowif you prefer to check for nulls instead of exception handling.