# Pre allocate memory

**URL:** <https://quantlab.discourse.group/t/pre-allocate-memory/51>\
**Category:** Qlang Language\
**Created:** [April 18, 2024, 7:38am UTC](https://quantlab.discourse.group/t/pre-allocate-memory/51 "2024-04-18T07:38:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casperfdr](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@casperfdr](https://quantlab.discourse.group/u/casperfdr)\
**Post date:** [April 18, 2024, 7:38am UTC](https://quantlab.discourse.group/t/pre-allocate-memory/51/1 "2024-04-18T07:38:08Z")

</div>

I need to store alot of data in a vector but don’t know in advance how much. Is there a way to pre allocate memory for my vector? What is best practice in these cases?

---

<div class="post-metadata">

**Author:** ![QuantBob](https://yyz1.discourse-cdn.com/flex003/user_avatar/quantlab.discourse.group/quantbob/32/9_2.png) [@QuantBob](https://quantlab.discourse.group/u/QuantBob)\
**Post date:** [April 18, 2024, 7:45am UTC](https://quantlab.discourse.group/t/pre-allocate-memory/51/2 "2024-04-18T07:45:31Z")

</div>

When creating the vector a fixed size pre-allocation can be used.

vector(number) x[mystartsize];

Along the way you can then resize or concat at the end. (Or push\_back). However these operations will create copy(ies) of the original.
