How are variables stored in memory.
Ok so i'm just trying to clear up how different variables are stored in memory/on the stack.
Say there is a program with global variables, a function that has varibale declared within it, and a main function that has variables declared in it and a call of the other function.
What I want to know is in what order these variables will be allocated and deallocated.
I imagine it's like this:
Global variables are allocated to memory
Variable within main function allocated to memory (assuming declaration is at the start of main, before the other function is called)
Function is now called, and variable within this are allocated to memory. Return value allocated to memory.
Variables within function deallocated
Return falue from function deallocated
Return value of main allocated to memory
Variables of main deallocated
Global variables deallocated.
Apologies if my terminology is incorrect. Would this seem the correct order for variables to be pushed/pulled to and from the stack? Or should variables from the functions be allocated when the function is first declared? Or am I correct in thinking they should only be allocated when the function is called in the main function?