Hi all.
I have two questions:
1. As I understand , when creating a local variable inside a function it is created on the stack, and when that specific function exits this variable is gone and the memory is freed (right so far..?).
But what happens when I return that variable from that function and use it somewhere else in the program ? like save it inside a global array for example... Is it still remaining on the stack ??
That means that If I have a very large global array that I use in my program , I should not populate it from variables I return from a function ?
for example , If I hold a very large global array of structs (so I can read from it during the program run):
2. As a sequel to the previous question, where global variables get allocated ? is it on the stack too ?Code:struct moveList{ int moves[7]; int size; }; struct moveList arr[900000]; //(is this also allocated on the stack??) //and this next function doing some processing and returns a struct: struct moveList getStruct(int x){ struct movesList mList; //doing something... return mList; } void allocateArray(){ int i; for(int i = 0; i < 900000; i++){ arr[i] = getStruct(i); } }
If a have a global array (like in the previews example), and it is allocated on the STACK.. but all of it's members are allocated on the HEAP. what does that means ? Is there some kind of "mixing" of STACK and HEAP ? (I get a little confused)
Should I allocate such a big array on the HEAP instead ?
My biggest worry is that memory that is allocated on the heap can be a little slow to read from.. and I need very fast access to that table !! what is your recommendation ?
Thx For all info