Arrays of floats in stack
I'm creating a structure to represent a 4x4 matrix and want it to be, at least, allocated entirelly on the stack. I also need to be able to get the array pointer in a unsafe context.
There is some way to store a array-like value like this on the stack instead of the heap?
48 Replies
There are fixed-size arrays and
stackalloc
ref structs is also a thing
you can put a span as a field in a ref struct
to get the pointer, use fixed
to make a struct that is the object on the stack and not a reference to this object, the only way are fixed-size arrays
or listing the amount of fields you need manually
which people actually do more often for this
like a vector field for each row
or a float field for each cellwhy do you want it on the stack?
stackalloc and ref structs are only allowed on the stack and would limit how you can use the data
if you need more, then you can create a struct with sequential layout with 16 fields in it
which is how every math library i've seen does it
that's what InlineArray is for
another neat thing about InlineArray is that passing it by
ref
will be slower if you've a large enough matrices, instead you pass them directly as raw Span<T>
or ROS<T>
and it will be faster than by ref passing manually,
and as you said for pointer stuff, you can just do this
then you can offset it with Unsafe.Add()
OR just by slicing them as span 🙂
easy peasy with inlinearray
the main issue with large sturcts has always been defensive copies, if you somehow rolling your own fat custom matrices (NOT the inlinearray way I shown above) make sure you tag each of modifying method with readonly
e.g : public readonly void DoSomethingThatWillMutateTheMatrix(T foo, T foo1)
including all your getters
, yes you're reading it right, getters
like so
i already tried stackalloc, but it asks for the structure to be a ref struct. a structure marcked with ref is still allocated on stack? i never understood how exactly ref structures works...
if you just make a local variable of a struct type, it will be on the stack
oh, this is new for me. will give a look
ref is not required
ref is for when you want to have a ref field in the struct
or a span field
stackalloc
is for a stack arraywhy marking the struct as ref is needed to have a field marked as ref?
because refs are only allowed on the stack. while regular structs can be on the heap too, ref structs can only be on the stack
:0 waaaaat lol this is really new to me i need to study about
thx
you're welcome
um, to be clear, not ref as in a class reference
ref as in e.g.
ref int a = ref otherInt;
@tired_weirdo_girlohh ok ok
@Anton here what i get

also i really cannot get what it wants...

where is that code
show context
sry i forgot, here the entire struct declaration

stackalloc
is only usable in methods
+ if you want a span field, it's going to have to be a ref struct
use a fixed buffer for what you're trying to do rn, that's what it's for
but it's honestly really pathetic
I haven't used the attribute that the person before suggested, but it might be betterohh nice this is new to me
i'm giving a look on the attribute rn, after i see if the fixed buffer is better
the fixed buffer is pathetic is what I meant to be clear
as a language feature
it's poorly designed
i can see
it needs to be unsafe because "CLR can't understand fixed buffers" or something like it?
lol this is just stupid i need to say
skip all this, just use InlineArray ffs 😄
lol fair
simply bcos InlineArray is already a Span.. technically
I already gave you examples how to span them up
and you want to vectorize that Matrix4x4 with pretty much ease, also gave example of it already
vectorize or bust, bcos if you're not, your matrix4x4 will just be the super slow version of System.Numerics.Matrix4x4 😄
any reason why you want to make your own custom matrix4x4 here?
i'm making a little help library for making games from scratch
then why not just use System.Numerics ones?
i don't like Matrix4x4 so much cause their methods asks for lots of parameters that usually are constant and i can't directly get the ptr of it to send though the functions of the unmanaged methods
i think it would be nice if i can do it without giving more shit to the GC
what?...
XD in short Matrix4x4 is too complex
i want something more simple and access it in low level
bcos they're marked as
readonly
for almost all the methods, to prevent defensive copies fucking up your game or projecti know, i'm marking mines too
I don't get it, you can simply just do this
but at the end of the operation i still need to create a array to get the data as it
wait i can :bigthonk:
System.Numerics.Matrix4x4 and all of them matrices has
CopyTo
method, which you can feed your own Span or array
yeah I don't think you should make your own here tbfh, reason being most nugets or external libs are using System.Numerics maths
surely can 😄
note the mat.x
there was meant to point the very 1 byte
I forget what numerics.matrix4x4 uses, so look the source and report back so we can help youwow looks like this works...

lol, yeah just dont forget pinning it with
fixed
and avoid Unsafe.AsPointerlooks like fixed is not needed
something about M11 be already fixed
errr... it will be needed, if it moves byt the GC you're fucked
thats why we need to pin them
ik, bit like
the language don't allows me to put a fixed on it lol
what

take the span out instead, so you dont need the fixed, then do this
better be safe
well, i still need to send it to unmanaged code that asks for a float*...
but maybe it has some kind of overload for spans or idk
there's Unsafe.GetPointer or something
iirc
either way you shouldn't be doing this, take the span approach instead
okk
really thx
@tired_weirdo_girl
make that an extension for your matrix or whatever
the
this
is your structright right
that should be pretty fast
also size matters
kinda surprised Numerics.Matrix4x4 doesnt have that tbfh 🤔