Program Address Space Stack and Heap 1 Program

  • Slides: 13
Download presentation
Program Address Space, Stack, and Heap 1

Program Address Space, Stack, and Heap 1

Program Address Space • Any program you run has, associated with it, some memory

Program Address Space • Any program you run has, associated with it, some memory which is divided into: – – Program code Data Segment or static data Stack Heap 0 x. FFFF stack SP address space heap (dynamically allocated) static data 0 x 0000 program code (text) PC

Program Address Space (Typical) 0 x. FFFF stack SP address space heap (dynamically allocated)

Program Address Space (Typical) 0 x. FFFF stack SP address space heap (dynamically allocated) static data 0 x 0000 program code (text) PC 3

Program Address Space (Typical) • Program code (Text): Also referred to as the application’s

Program Address Space (Typical) • Program code (Text): Also referred to as the application’s code or binary. It contains the application’s executable instructions and is usually read-only and fixed in size. • Static data: Also referred to as Data Segment, contains the application’s statically –allocated (as opposed to dynamically-allocated) data. 4

The Stack • Stack memory – memory part in which items are added and

The Stack • Stack memory – memory part in which items are added and removed in last-in, first-out order. • The stack is where local variables are allocated. It is also where function parameters and return addresses are placed. • The stack grows downward. 5

“Stack”: How it works • Called routine • Push registers and return address onto

“Stack”: How it works • Called routine • Push registers and return address onto stack • Push temporary storage space onto stack • Do work of the routine • Pop registers and temporary storage off stack • Leave result on stack • Return to address left by calling routine 6

Process Address Space (Typical) 0 x. FFFF stack SP address space heap (dynamically allocated)

Process Address Space (Typical) 0 x. FFFF stack SP address space heap (dynamically allocated) static data 0 x 0000 program code (text) PC 7

Heap • The heap is where dynamic data is allocated using: • new objects

Heap • The heap is where dynamic data is allocated using: • new objects in C++, Java, etc. • malloc in C 8

Dynamically Allocating from Heap • New or malloc() • Allocates a chunk of memory

Dynamically Allocating from Heap • New or malloc() • Allocates a chunk of memory of desired size • Returns pointer • Delete or free () • Returns previously allocated chunk to heap for reallocation 9

Stack and Recursion • Imagine the following program: – int factorial(int n){ if (n

Stack and Recursion • Imagine the following program: – int factorial(int n){ if (n <= 1) return (1); else int y = factorial(n-1); return (y * n); } • Imagine also the caller: – int x = factorial(100); • What does compiled code look like? 10

Compiled code: the caller int x = factorial(100); • Put the value “ 100”

Compiled code: the caller int x = factorial(100); • Put the value “ 100” somewhere that factorial function can find • Put the current program counter somewhere so that factorial function can return to the right place in calling function • Provide a place to put the result, so that calling function can find it 11

Compiled code: factorial function • Save the caller’s registers somewhere • Get the argument

Compiled code: factorial function • Save the caller’s registers somewhere • Get the argument n from the agreed-upon place • Set aside some memory for local variables and intermediate results – i. e. , y, n - 1 • Do whatever factorial was programmed to do • Put the result where the caller can find it • Restore the caller’s registers • Transfer back to the program counter saved by the caller 12

Question: Where is “somewhere”? • So that caller can provide as many arguments as

Question: Where is “somewhere”? • So that caller can provide as many arguments as needed (within reason)? • So that called routine can decide at run-time how much temporary space is needed? • So that called routine can call any other routine, potentially recursively? 13