if i have something like this, would it be better to declare the array outside of the loop for memory allocation reasons? or does it make no difference?
if i allocate it outside the loop than i can REUSE the same array for all loops, but does the compiler optimize it such that its the same thing in the end?
EDIT:
actually my parse function creates an array anyway and returns it so… idk lol does it matter then if i declare a new array currentShapeAr, and also declare an array in the Parse function that returns to be assigned to currentShapeAr?
EDIT2:
actually the number of elements is fixed at 3, so i can just create a 3 element array and pass it as parameter to the Parse function and assign the parsed string to the array directly, so at least i dont have to create 2 arrays every time.
Arrays are reference types. You’re not allocating here you’re just getting a reference to the return of ParseShapeDetectString(arShapeDetect[shapeArIndex]), unless said method is returning a new array.
In any case use the profiler to check the differences.
yea i changed code so i just pass my array currentShapeAr declared and initialized outside the loop with 3 elements fixed.
So the array is only created once outside loop within function scope, so itll get deallocated after while loop completes and function closes right?
But i am confused by reference type, if its not allocated here where is it allocated then?
EDIT:
haha just realized had to pass the array into function using “ref” keyword otherwise assigning the parsed strings into the array only does it locally in the Parse function, this really screws with my head, it feel to me like im passing the actual array from outside into the Parse function, but somehow it just assigns the new strings locally to a locally created array thats the same as what i passed into the parameter? but outside it stays the same!? it just doesn’t make sense to me.
ClassA a1 = new ClassA(); // allocates
ClassA a2 = a1; // does not allocate
a2 is just a reference to the same point in memory as a1 instanced, but you’re not allocating memory as you do so.
So it depends on what ParseShapeDetectString is returning. Is it returning a newly instantiated array? Or is just returning a reference to an existing array in memory?
oh duhhh yea of course it allocates on new, brain fart moment.
yea i changed up my code, before my Parse function initialized a new array and assigned it to Split(“_”); and then returned it to be assigned to my outside array which was also initialized with same number of elements so it worked.
Now i just declare an empty 3 elements array outside the loop, and ParseShapeDetectString(ref currentShapeAr, arShapeDetect[shapeArIndex]);
so with “ref” keyword I can directly assign the passed array to Split(“_”);
but where is the array initialized, on the stack?
if i did a new array initialization inside the while loop, how would the compiler handle that?