# Unable to allocate a large size of memory with VtkUnsignedShortArray

**URL:** <https://discourse.vtk.org/t/unable-to-allocate-a-large-size-of-memory-with-vtkunsignedshortarray/8374>\
**Category:** Development\
**Created:** [April 25, 2022, 6:47pm UTC](https://discourse.vtk.org/t/unable-to-allocate-a-large-size-of-memory-with-vtkunsignedshortarray/8374 "2022-04-25T18:47:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PBH](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/e480ec/32.png) [@PBH](https://discourse.vtk.org/u/PBH)\
**Post date:** [April 25, 2022, 6:47pm UTC](https://discourse.vtk.org/t/unable-to-allocate-a-large-size-of-memory-with-vtkunsignedshortarray/8374/1 "2022-04-25T18:47:02Z")

</div>

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

```
	vtkSmartPointer<vtkImageData> image =
		vtkSmartPointer<vtkImageData>::New();
	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.

---

<div class="post-metadata">

**Author:** ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)\
**Post date:** [April 25, 2022, 7:13pm UTC](https://discourse.vtk.org/t/unable-to-allocate-a-large-size-of-memory-with-vtkunsignedshortarray/8374/2 "2022-04-25T19:13:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PBH](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/e480ec/32.png) [@PBH](https://discourse.vtk.org/u/PBH)\
**Post date:** [April 26, 2022, 12:49am UTC](https://discourse.vtk.org/t/unable-to-allocate-a-large-size-of-memory-with-vtkunsignedshortarray/8374/3 "2022-04-26T00:49:50Z")

</div>

> [@PBH](#):
>
> 39583744

Thanks! I misunderstood API.
