# Allocate Array of objects

**URL:** <https://forum.crystal-lang.org/t/allocate-array-of-objects/2082>\
**Category:** Help & Support\
**Created:** [May 9, 2020, 12:58am UTC](https://forum.crystal-lang.org/t/allocate-array-of-objects/2082 "2020-05-09T00:58:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 9, 2020, 12:58am UTC](https://forum.crystal-lang.org/t/allocate-array-of-objects/2082/1 "2020-05-09T00:58:15Z")

</div>

I’d like to experiment with allocating objects in a sequential group vs fragmented objects with sequential algorithms.

What I have so far:

```auto
ary = Array(A).new(n)
ptr = Pointer(A).malloc(n)
n.times { ary << ptr.as(A); ptr += 1 }
# TODO: finalizer

```

How do I call `initialize` method? Is there a better way to do this?

The number is determined at runtime.

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 9, 2020, 2:42pm UTC](https://forum.crystal-lang.org/t/allocate-array-of-objects/2082/2 "2020-05-09T14:42:52Z")

</div>

It’s just a method you call on the object, so `.value.initialize`.

To be honest this all sounds very YAGNI :D So I’m not sure about any better ways. FWIW `Array` is completely implemented in Crystal, there’s no compiler special casing for it beyond the literal syntax, so for example here’s the method to allocate a new array all pointing to the same value: [https://github.com/crystal-lang/crystal/blob/master/src/array.cr#L105-L118](https://github.com/crystal-lang/crystal/blob/master/src/array.cr#L105-L118)

For the finalizer there’s `GC.add_finalizer`, though you probably want some kind of wrapper struct where you can iterate through the childrens to call finalize on them when the entire object goes out of scope.

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 9, 2020, 4:59pm UTC](https://forum.crystal-lang.org/t/allocate-array-of-objects/2082/3 "2020-05-09T16:59:46Z")

</div>

@jhass

```auto
obj.initialize
Error: protected method 'initialize' called for A

```

`Array.new(n, obj)` works for `Struct`, but not `Object`. The same object reference occupies all slots.

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 9, 2020, 5:09pm UTC](https://forum.crystal-lang.org/t/allocate-array-of-objects/2082/4 "2020-05-09T17:09:01Z")

</div>

Yes, I didn’t mean to say it’s equivalent, just similar.

Well I guess you want to make a public method on `A` or `Object` then that calls initialize.

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 9, 2020, 5:11pm UTC](https://forum.crystal-lang.org/t/allocate-array-of-objects/2082/5 "2020-05-09T17:11:18Z")

</div>

Right now I’m at the benchmark stage. If it don’t see a signifiant improvement I’ll toss it.
