Unable to allocate a large size of memory with VtkUnsignedShortArray

The following codes cause exception: “vtkUnsignedShortArray (0000014ECA3A0510): Unable to allocate 1566872789057536 elements of size 2 bytes”.

	vtkSmartPointer<vtkImageData> image =
	image->SetDimensions(512, 512, 151);
	image->AllocateScalars(VTK_UNSIGNED_SHORT, 512*512*151);

Note that 512x512x151 is 39583744, much less than 1566872789057536. This happens in both VTK 7.1 and VTK 9.0. It works for small size of memory such as 10000. Any help is greatly appreciated.

The second parameter of AllocateScalars is numComponents, i.e. the number of components in the Scalars array (see vtkDataSetAttributes for more information on what is the Scalars array).

What you asked was to allocated an array of 512 * 512 * 151 tuples, each having 512 * 512 * 151 components. If you want one component, you should call image->AllocateScalars(VTK_UNSIGNED_SHORT, 1), and this should work.

Thanks! I misunderstood API.