As Delta_G pointed out, your method of assigning a pointer to an array that will essentially be immediately destroyed won’t work.
Aside from the academic exercise, why do you need to create this random-length array... to what end?
You could consider reserving a static array in the class sized to the maximum allowable. In that case, only one array gets created regardless of the number of class instances.