Questions about Struct

reading Structs - Crystal

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 ?

def foo
  arr = [] of Int8 | Int32 | String | Point
  arr <<,2)
  arr <<,4)
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?

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.

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.