Today?s topics Program Memory Stacks and heaps Process Address Space Two types of Memory Stack: Automatically allocated and deallocated Local variables, parameters Used to implement function calls Heap: Manually allocated and deallocated (sometimes not!) Dynamic sized data structures Used for data structures whose size is hard to predict Example C program int main() { int a; a=f(); } int f() { int a,b; a=10; b=10; return (a*b); } Example C program int main() { int a; a=f(); } int f() { int a; a=10; return (a*g()); } int g() { int a; a=100; return (a); } Fibonacci Numbers int fib (int n) { if (n < 0) return 0; if (n == 0) return 1; if (n==1) return 1; return (fib(n-1)+fib(n-2)); } Slightly Modified int main() { int i; int *c = (int *)malloc(10*sizeof(int)); for (i=0; i<10; i++) c[i] = i*i; free(c); } Linked Lists Computer Game Maintain list of all moves made by the player Memory requirement unknown Use dynamic memory, or the ?heap? Where would the following go? Parameters, Return values Global Variables Garbage Collection No need to explicitly deallocate memory i.e. no need for free() or equivalent No memory leaks Automatic Garbage Collection http://sleibowitz.home.att.net/CPP_Java/AGC.html A key feature of Java is its garbage-collected heap http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html Summarizing Stack grows and shrinks implicitly The heap grows and shrinks in response to malloc() and free() calls Space taken by global variables remains constant throughout Back to Processes: The Address Space of an Unix Process text data bss dynamic stack More about Different Segments: http://www.informit.com/articles/article.asp?p=173438 Process Address Space Contents of registers Other stuff ? open files, etc How is a new process created? Creating a Process: Before fork() fork( ) parent process Creating a Process: After fork() fork( ) // returns p parent process fork( ) // returns 0 child process (process id = p) exec(): Loading a New Image exec(prog, args) Before prog?s text prog?s data prog?s bss args After System Calls For Process Management
STUDYBLUE makes things that make you better at school.
Things like
online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free.
Join us.
“I have used this website for three exams, and I see a huge difference in my test results.”
Naj