Download presentation
Presentation is loading. Please wait.
1
Object Lifetime and Pointers
various languages…
2
Why do we care? Could affect performance Could affect reliability
Could affect language choice
3
Object lifetime The lifetime of a variable is the time during which it is bound to a particular memory cell Ruby built-in objects created when values assigned (e.g., x=5) Other classes created with new factory methods also create objects Ruby uses garbage collection to destroy objects that are no longer reachable Quick discuss: why would you expect a language like Ruby, which has dynamic typing, to also have garbage collection? Answer: since Ruby is in charge of allocating space as needed, it also needs to reclaim space.
4
Object Lifetimes static stack dynamic explicit heap implicit heap
5
Variables by Lifetime: Static
bound to memory cells before execution begins remains bound to the same memory cell throughout execution all FORTRAN 77 variables, C static variables (not C++ class variables) Advantages: efficiency (direct addressing) history-sensitive subprogram support Disadvantage: lack of flexibility (no recursion) storage can't be shared among subprograms void myFn() { static int count=0; count++; cout << count; } myFn(); Quick Ex – turn in Trace! Discuss bullets Draw pic of direct addressing
6
uninitialized data (BSS)
Where is static stored? Assuming C/C++ DATA segment subdivided into parts when loaded into memory high address p temp temp2 command-line args & environment variables stack heap initialized by exec (block started by symbol) uninitialized data (BSS) initialized data text read from program file by exec low address From:
7
Variables by Lifetime: Stack Dynamic
Created when execution reaches code Allocated from the run-time stack Variables may be allocated at the beginning of a method, even though declarations may be spread throughout Advantages: allows recursion conserves storage Disadvantages: Overhead of allocation and deallocation (not too bad, since all memory allocated/ deallocated together) Subprograms cannot be history sensitive Inefficient references (indirect addressing) void myFn2(int parm) { int temp; … int temp2; } How? Compared to what? parm temp temp2 sp local
8
Variables by Lifetime: Explicit Heap Dynamic
Allocated (and deallocated) by explicit directives during runtime new/delete, malloc/free etc. Accessed only through pointers or references Dynamic objects in C++, all objects in Java Advantage: provides for dynamic storage management Disadvantages: inefficient and unreliable C# methods that define a pointer must include reserved word unsafe void myFn3() { int* nums = new int[5]; … } public void myFn4() Point point = new Point();
9
Variables by Lifetime: Implicit Heap Dynamic
Allocation and deallocation caused by assignment statements No new/delete… these are implied! all variables in APL; all strings and arrays in Perl and JavaScript Advantage: flexibility Disadvantages: Inefficient because all attributes are dynamic loss of error detection list = [2, 4.33, 6, 8]; Which lifetimes are used in Ruby?
10
Pointers and References
11
Pointers vs References
A pointer type variable has a range of values that consists of memory addresses and a special value, nil or NULL Provide a way to manage dynamic memory A pointer can be used to access a location in the area where storage is dynamically created (usually called a heap)
12
To Heap or Not In C++ it is not necessary for all pointers to reference heap. Write a few lines of code to show this.
13
Pointer Operations – (review)
Two fundamental operations: assignment and dereferencing Assignment is used to set a pointer variable’s value to some useful address Dereferencing yields the value stored at that address
14
Pointer Assignment and Dereferencing Illustrated (review)
Provide the power of indirect addressing (access variable via address stored in another variable, may not be dynamic) ptr = new int; // assignment *ptr = 206; // dereferencing j = *ptr; // dereferencing
15
Pointer Operations – (review)
Dereferencing can be explicit or implicit C++ uses an explicit operation via * j = *ptr; sets j to the value located at ptr (*ptr) = 5; sets the value located at ptr to 5 C++ also does implicit dereferencing of reference variables void myFun(int& x) { x = 5; } What about Java?
16
Pointer Arithmetic in C and C++
float stuff[100]; float *p; p = &stuff; int i=3; 1 2 3 4 5 6 7 8 stuff 0x100 p x100 p is an alias for stuff *(p+5) is equivalent to stuff[5] and p[5] *(p+i) is equivalent to stuff[i] and p[i]
17
Pointers in C and C++: void*
Domain type need not be fixed (void *) void * can point to any type. Use type casts. void * cannot be de-referenced void * often used in C to pass as arguments. In C++, generally better to use templates so compiler can do appropriate type checking. Remember generic programming units? Early solution
18
Quick Question Do you remember the difference between a dangling pointer and a memory leak?
19
Problems with Pointers (review)
Dangling pointers (dangerous) A pointer points to a heap-dynamic variable that has been de-allocated may have been reallocated values no longer meaningful writing to it could cause storage manager to fail. Example Point p = new Point(3,4); delete p; // dangling – p still has address! cout << p.getX(); // bad!!
20
Problems with Pointers (review)
Memory Leak (dangerous) Memory has not been deleted (returned to heap manager) No variables contain the address (so not accessible) When is this a problem? Programs written for school? no.. Long-running programs like web servers? yep… Example int[] p = new int[5000]; p = new int[10000];
21
Reference Types C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters Constant pointer that is always implicitly dereferenced (notice no * in this code) void myFun(int &y) { y = y + 1; } What does this mean, “constant pointer”?
22
Reference Types (point of confusion)
Constant pointer – can’t change where it points (can change contents) Java extends C++’s reference variables and allows them to replace pointers entirely References refer to object instances – but not necessarily constant (i.e., can change address it references) Implicitly dereferenced (i.e., no * required) No pointer arithmetic Java does NOT have pointers! But references as implemented in Java have many similarities. C# includes both the references of Java and the pointers of C++.
23
What about Ruby? Does Ruby have references or pointers?
Ruby has garbage collection. What problem does garbage collection solve? (dangling pointer? memory leak?)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.