Language Concepts

advertisement
various languages…
 Could
affect performance
 Could affect reliability
 Could affect language choice
 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
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();
myFn();
myFn();
Quick Ex:
• Trace!
• Discuss bullets
Assuming C/C++
 DATA segment subdivided into parts when loaded into memory
command-line args &
environment variables
high address
stack
p
temp
temp2
heap
low address
uninitialized data
(BSS)
initialized by exec
(block started by symbol)
initialized data
read from program file
by exec
text
From: http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c
Stack-dynamic




void myFn2(int parm)
{
int temp;
…
Created when execution reaches code
Allocated from the run-time stack
Variables may be allocated at the
beginning of a method, even though
int temp2;
declarations may be spread
}
throughout
Advantages:
• allows recursion How? Compared to what?
• 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)
sp
parm
temp
temp2
local
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();
}
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:
list = [2, 4.33, 6, 8];
• flexibility

Disadvantages:
• Inefficient because all attributes are
dynamic
• loss of error detection
Which lifetimes are used in Ruby?
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)
In C++, is it necessary for all pointers to access heap?



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 the location
represented by the pointer’s value
• Dereferencing can be explicit or implicit
• C++ uses an explicit operation via *
j = *ptr;
sets j to the value located at ptr
• C++ also does implicit dereferencing
void myFun(int& x) { x = 5; }
What about Java?
Provide the power of indirect addressing (access variable via
address stored in another variable, may not be dynamic)
ptr = new int;
*ptr = 206;
j = *ptr;
// assignment
// dereferencing
// dereferencing
float stuff[100];
float *p;
p = &stuff;
int i=3;
p
stuff 0x100
0x100
0
1
2
3
4
5
6
7
8
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]





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.
 Do
you remember the difference
between a dangling pointer and a
memory leak?
 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!!
 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?
 Example
int[] p = new int[5000];
p = new int[10000];

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
void myFun(int &y)
{
y = y + 1;
}
What does this mean, “constant pointer”?


Java extends C++’s reference variables and allows
them to replace pointers entirely
• References refer to object instances – not necessarily
constant
• Implicitly derefenced (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++.
 Does
Ruby have references or pointers?
 Ruby
has garbage collection.
What problem does garbage collection solve?
http://stackoverflow.com/questions/7208768/is-it-possible-to-use-pointers-in-ruby
Download