Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory in Computer Yung-Hsiang Lu Purdue University 1.

Similar presentations


Presentation on theme: "Memory in Computer Yung-Hsiang Lu Purdue University 1."— Presentation transcript:

1 Memory in Computer Yung-Hsiang Lu Purdue University 1

2 C vs. Newer Languages When C was developed (late 1960s to early 1970s), computers were slow. Thus, C's design principle is "A C program does exactly what it is instructed to, nothing more." C does not initialize variables, i.e., uninitialized variables contain uncertain values. C does not release memory, i.e., no garbage collection like Java. C does not check errors. Programmers are responsible for correctness. 2

3 Why Do Computers Need Memory? Imagine that you are solving a complex problem. You need some reference books (also called "read-only memory") and some scratch paper. "Memory" in computers is the reference books and scratch paper Computers were very expensive and memory was precious. Thus, memory management is an important topic when writing computer programs 3

4 4 This core memory is about the size of your mobile phone. Your phone can store about 10,000,000 times more data.

5 Memory in Computers User Space (can be used by ordinary programs) Kernel Space (only operating system can use) 5

6 User Space Memory Stack Memory Heap Memory Program Memory 6

7 User Space Memory Stack Memory Follow "first-in last- out" rule Heap Memory Can be allocated and released by programs Program MemoryStore programs 7

8 Stack Memory is a type of memory inside computers strictly follows the "first-in last-out" (or last-in first-out) rule is indirectly controlled by your programs is directly controlled by compilers and operating systems 8

9 What is stack? "Stack" means what comes first leaves last. You are using this concept everyday. You put on socks before putting on shoes. You take off the shoes before taking off the socks. You put on a shirt before wearing a jacket. You take off the jacket before taking off the shirt. When you put a book on the top of a pile, the last added book is removed first. 9

10 Stack Memory in Programs Consider this snippet of code: f1 calls f2 and f2 calls f3 f1 is seen first, then f2, then f3 When f3 finishes, the program continues from f2, not f1 10 f1() { f2(); } f2() { f3(); }

11 Stack Memory in Programs The stack memory has only one function, i.e., f1 11 f1() { f2(); } f2() { f3(); } If the program is here f1

12 Stack Memory in Programs Conventionally, the stack memory starts from bottom and grows upwards. The stack memory has two functions now, i.e., f1 and f2 12 f1() { f2(); } f2() { f3(); } If the program is here f2 f1

13 Stack Memory in Programs After f3 finishes, the program continues f2. 13 f1() { f2(); } f2() { f3(); } If the program continues here f2 f1

14 Stack Memory in Programs After f2 finishes, the program continues f1. 14 f1() { f2(); } f2() { f3(); } If the program continues here f1

15 That's the idea. The next lecture will be more precise and get into the details 15


Download ppt "Memory in Computer Yung-Hsiang Lu Purdue University 1."

Similar presentations


Ads by Google