Recently for fun I have decided to build a toy programming, compiler and vm. While starting to implement the virtual machine I got stuck. The stack which holds the variables and structs I implemented as separate arrays for each type. The problem is when I have a reference to a struct the elements are not aligned, int struct.x might be at address 2, and float struct.y might be at address 56, so accessing the struct by a reference would be impossible, because the indexes are not linear. How could I solve this?
edit:
first of all for each type I mean for each primitive, and second I know I could implement it with unions but I want to learn how it is really implemented in java, c++ or c#, that's kind of the point of making a toy language, to better understand what you are programming.