# Questions about Struct

**URL:** <https://forum.crystal-lang.org/t/questions-about-struct/6268>\
**Category:** Help & Support\
**Created:** [January 7, 2024, 9:13am UTC](https://forum.crystal-lang.org/t/questions-about-struct/6268 "2024-01-07T09:13:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![femto](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/femto/32/2246_2.png) [@femto](https://forum.crystal-lang.org/u/femto)\
**Post date:** [January 7, 2024, 9:13am UTC](https://forum.crystal-lang.org/t/questions-about-struct/6268/1 "2024-01-07T09:13:38Z")

</div>

reading [Structs - Crystal](https://crystal-lang.org/reference/1.10/syntax_and_semantics/structs.html)

# The array’s buffer will have 8 bytes dedicated to each Point

ary = of Point  
is a bit confusing to me.  
first struct is allocated on stack, wouldn’t that mean when methods returns struct maybe destroy ?

```auto
def foo
  arr = [] of Int8 | Int32 | String | Point
  arr << Point.new(1,2)
  arr << Point.new(3,4)
end
puts foo #when foo returned, wouldn't the existing stack be destroyed?

```

Also alongside with the question is how arr stores them, so ary can be  
an union of different size, how ary is possible to store different sizes  
since not all of them are reference?

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [January 7, 2024, 12:35pm UTC](https://forum.crystal-lang.org/t/questions-about-struct/6268/2 "2024-01-07T12:35:23Z")

</div>

A union with value types have the first 4 bytes store an UInt32 that is the type identifier of the rest of the bytes to represent the largest payload of the union types.

Unions with only reference types are just a pointer as usual since type information exist in the object representation.

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [January 7, 2024, 7:12pm UTC](https://forum.crystal-lang.org/t/questions-about-struct/6268/3 "2024-01-07T19:12:25Z")

</div>

> [@femto](#):
>
> first struct is allocated on stack, wouldn’t that mean when methods returns struct maybe destroy ?

Any struct types are passed by value rather than by reference, so even if the stack frame they were instantiated in is no longer in scope or even if that entire stack is destroyed (for example, if they were instantiated in a separate fiber), there is a _copy_ of it inside the array buffer.

Unless you’re getting way too clever (like using pointers), struct instances can’t be out of scope.
