   difference between static and stack memory.
  Hi,In the C context,I always had difficulty understanding text books referring distinctively to the static and stack memory. What is the difference?Thanks in advance.   
  Simple version.Each thread has its own stack.Static memory is one for all.   ;  In C (which is quite close to machine representation):Static memory = global variables (or any variables with static specifier which includes local variables with static keyword).Stack memory = local variables (without static specifier).It's called static because it's there all the time from when the program is loaded until it quits. It can't "die".Stack allocated variables die when the function they are declared in exits. They are instantiated on function entry. That also means a stack allocated variable as seen in the code refers an instance specific to the function invokation, whereas a static variable always refers to a single global instance.Generally, for application level C programming you should pretty much avoid static variables because using them causes problems with concurrent execution and extensibility.Edit: Ninja'd   ;  Thanks pals, so basically stack memory is where automatic variables are allocated at?   ;  Yeah (unless my definition of automatic is different from yours).Btw funny seeing my rating drop in real time, presumably because of my harsh judgement on globals. ;) (Pre-emptively: There are exceptions, but go with stack and heap allocation by default.)   ;  You have a program. It consists of functions. At any point, execution is "inside" a function (determined by the process's program counter). A function can call another function. When this happens, execution changes to be inside of the called function.A function can return to the point where it was called and resume execution there. Therefore we need to keep track of which functions call which. To add one last detail, a function has the option to call itself (we call this a recursive function), so it is possible to have two "copies" of the same function being executed at different points.This is the scenario we're dealing with (slightly simplified, because things like threading make it a little more complicated).The technique we use to handle this is called a stack frame (also called an activation record). Whenever a function is called (including the main function in a program), we create a new stack frame. The stack frame is a kind of bookkeeping for function calls. Typically the stack frame contains:* the return address - the address of the instruction immediately following the function call that created this stack frame.* the parameters - the actual parameters passed to the function are copied to the stack frame. * scratch space - memory reserved for the callee. This is where local variables are stored.When you call a function, the stack frame is created with this information and pushed onto a stack. The program counter then jumps to the function's address in memory, and the new function continues. The function may access local variables by inspecting the stack frame. The function may also use the scratch space to store any local/temporary variables it needs to do its job.When the function needs to return to its caller, it simply grabs the return address from the stack frame, pops the stack frame, and then jumps to that address.Under this model, functions are always properly nested. They always know who called them. They have access to their own copy of the parameters and they have space to work with temporary variables. It's all very nice :)This is pretty much all there is to what you call "stack memory" as you call it. Global data is much, much simpler. There's one copy of a variable for the whole program. It's not stored in any activation record. When you change it, it changes throughout your program -- whereas in contrast, changing a parameter only changes that value in the current stack frame. Because global variables are simpler, new programmers will tend to want to use them. The simplicity is nice for certain things, but stack-allocated variables are much more flexible for many tasks. ;  Thanks for the explanation.I know that there are stackless languages, like stackless Python, how they work is a mistery.   ;  Stackless Python is named so because it avoids using the C stack while executing. It still uses the stack, only less.For an explanation of how Stackless avoids the stack, see this excellent explanation on... Stackoverflow.  o_O'   ;Quote:Original post by gastoThanks for the explanation.I know that there are stackless languages, like stackless Python, how they work is a mistery.Python *does* use a stack.There is a variation of Python called Stackless Python, though, which does not rely on the same mechanism for function calls (just as Konfucius said). But regular CPython, as well as many other flavors (Unladen Swallow, PyPy, JPython, IronPython) all use a stack.The stack frame model isn't the only model of computation, mind you. It's just the most common. One advanced language construct available in functional languages is the continuation. A continuation is like a goto on steroids. I won't get into an explanation here (because they aren't available in C++ or C# or any of the languages people normally use for games). But one trait continuations have is they allow you to write programs where the stack frames form a tree instead of a stack. With continuations, you can pause execution on one function and jump to another.For something that's half way between these two are languages that support closures (Python... sorta, Ruby, &#106avascript, Lua). Because closures can capture references to variables on the stack, those variables need to continue to exist even after the function for that stack frame returns.Of course, for games and 90% of programming, you don't need to worry about any of that :) The stack is the hammer of your programming toolbox. It's simple, it doesn't solve every program, but every craftsman should master it.;Quote:Original post by Tac-Tics A continuation is like a goto on steroids. I won't get into an explanation here (because they aren't available in C++ or C# or any of the languages people normally use for games).C# absolutely has continuations. Forms of continuation-passing style are used quite a bit in C# code...   