Guest User

Untitled

a guest
Nov 12th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. RAM, is the memory part of your computer. So the original codes of your program is stored on the hard drive on your computer. When we want to run the program, all the codes of the program, and memory needed for all the variables in the program, will be loaded into RAM, so that everything will run a lot quicker and easier.
  2.  
  3. We thought of memory divided into 3 main parts.
  4.  
  5. On the top part, is called "TEXT". When you double click the program icon to run it, or in Linux, you type "./program_name" to run it, the machine code of your program, like the actural codes of your whole program translated into 0 and 1, those will be loaded into "TEXT" part of the RAM.
  6.  
  7. Dynamically allocated memory comes from a pool of memory known as the heap. Statically allocated memory will be in the memory area called stack.
  8.  
  9. Mostly if you give a variable a name, it will probably live in the stack. If you don't give variable a name, it will be at heap.
  10.  
  11. Every time you use "malloc()", the computer will go to heap section of the RAM and allocate memory.
  12.  
  13. C uses malloc() to dynamically allocate memory. When requested, malloc() will go to heap and look for memory blocks to allocate. After obtaining memory block for you, it doesn't give that memory block a name, it will just return a pointer to that memory.
  14.  
  15. Stack is for local variables and functions. Main() function takes some space in Stack. When another function is called, it will take up some memory on top of the function who called it in Stack. And same thing happens if another function is called.
  16.  
  17. So the memory space in Stack will gradually be taken, with one funtion on top of another. If there are too many functions called, the Stack might overflow the Heap, or the Heap might overflow the Stack. That's when things might not work because not enough memory, and you will have to quick some function in order for the computer to operate normally.
  18.  
  19. In another snippet titled "Recursion" explains in detail about how "Call Stack" works in RAM when a program is running:
  20.  
  21. Call Stack
  22.  
  23. When you call a function, the system sets aside space in memory for that function to do it's necessary work. We frequently call such chunks of memory Stack Frames or Function Frames.
  24.  
  25. More than one function's stack frame may exist in memory at a given time. If main() calls move(), which then calls direction(), all three functions have open frames.
  26.  
  27. These frames are arranged in a stack. The frame for the most recently called function is always on the top of the stack.
  28.  
  29. When a new function is called, a new frame is pushed onto the top of the stack and becomes the active frame.
  30.  
  31. When a function finishes its work, its frame is popped off of the stack, and the frame immediately below it becomes the new, active, function on the top of the stack. This function picks up immediately where it left off.
  32.  
  33. For example, use the recursive factorial function above, the whole program look like this:
  34.  
  35. int fact(int n)
  36. {
  37. // base case
  38. if (n == 1)
  39. {
  40. return 1;
  41. }
  42. // recursive case
  43. else
  44. {
  45. return n * fact (n - 1);
  46. }
  47. }
  48.  
  49. int main(void)
  50. {
  51. printf("%i\n", fact(5));
  52. }
  53.  
  54. So when this program runs, the main() gets called first, so main() will get a chunk of memory space, so it will be in the stack. After that, main() will call printf() function, so then printf() will become the active frame, and it will be on top of main() stack. When printf() is running, it will call fact(5) that's inside of it, so fact(5) will become active and goes on top of printf(). So it will go on like this, face(4), fact(3)...fact(1) will go on top of another.
  55.  
  56. So in the end fact(1) will be on top of the stack. Now fact(1) doesn't need to call any other function anymore, it can just return a value which is "1". So when fact(1) returns "1", it finished executing, so it will be popped off the stack. That means the chunk of memory space that was allocated to fact(1) is released because fact(1) has finished it's job.
  57.  
  58. So when fact(1) is popped off, fact(2) becomes the active frame. Because fact(1) has returned a value, so fact(2) is able to do it's calculation and return a value as well. So after fact(2) has finished executing, it will pop off the stack as well. This goes on and on, fact(3) will execute and return a value then pop off, then fact(4), fact(5), printf(), and the last main().
  59.  
  60. A good example of how Call Stack work and how to use memory addresses in programs, refer to the "swap()" program we talked about before. Refer to another snippet in "Memory" label titled "An example of how pointers pass variables between functions".
Add Comment
Please, Sign In to add comment