# Can I pre-allocate a vtkCellArray?

**URL:** https://discourse.vtk.org/t/can-i-pre-allocate-a-vtkcellarray/9695
**Category:** Support
**Created:** [October 29, 2022, 6:06am UTC](https://discourse.vtk.org/t/can-i-pre-allocate-a-vtkcellarray/9695 "2022-10-29T06:06:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![SpareSimian](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@SpareSimian](https://discourse.vtk.org/u/SpareSimian)
#### Post date: [October 29, 2022, 6:06am UTC](https://discourse.vtk.org/t/can-i-pre-allocate-a-vtkcellarray/9695/1 "2022-10-29T06:06:13Z")

</div>

I’m already pre-sizing my vtkPoints array with the number of points I know I’ll need. I know how many cells (vtkQuad for my app) I’ll need so is there a way to optimize my vtkCellArray allocation in the same way? I haven’t found an example that does this. They all use InsertNextCell. I see methods for vtkCellArray that suggest they’d do this but I’m not sure what combination to use.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [October 29, 2022, 12:37pm UTC](https://discourse.vtk.org/t/can-i-pre-allocate-a-vtkcellarray/9695/2 "2022-10-29T12:37:20Z")

</div>

I typically use vtkCellArray::ResizeExact() followed by a Visit() method to populate the cell array. Note that it helps to understand that, under the hood, you are actually populating two arrays: Offsets and Connectivity. Plus you have to understand that the cell array may be either 32 bit or 64 bit storage. It’s a little tricky at first, but the Visit() mechanism simplifies traversal, and the ability to have different cell array types can save a lot of memory.

---

<div class="post-metadata">

### Author: ![SpareSimian](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@SpareSimian](https://discourse.vtk.org/u/SpareSimian)
#### Post date: [October 29, 2022, 9:10pm UTC](https://discourse.vtk.org/t/can-i-pre-allocate-a-vtkcellarray/9695/3 "2022-10-29T21:10:57Z")

</div>

Aha, The docs for ResizeExact suggested I should use AllocateExact, and I didn’t see what I should use with ResizeExact to actually modify the new cells. I’ve already got a loop through my source data that’s compiling the points and quads, so I’ll need to rethink that to incorporate it into the Visit() functor. (I’m a fan of the visitor pattern so I’m looking forward to it. 😉 )
