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.