RU beehive logo ITEC dept promo banner
ITEC 320
2018fall
nokie
ibarland

ITEC320 closing thoughts

Some last examples on memory-allocation

Three areas of memory:

We will look at some interesting examples from Dr Okie's notes on memory layout/addresses:


So: What did we talk about, this semester?

See Dr Okie's page for a list of study-topics.

Is 320 about Ada?

No. Well, not much.
ITEC320 is about providing perspective on what a programming language can do, and can look like, to be contrasted with your (perhaps) first language/paradigm (Java and Object-Oriented).

Review: stack- vs. heap-allocation

Cons of heap-allocation
Cons of stack-allocation

Having a language which supports both immediate objects and object-references is its own mixed blessing: The programmer needs to be remembering/aware of whether each value can be aliased or not. Also, there can be mismatches between functions that want objects vs. references-to-objects, that must be converted. (This can be in functions written by your team, as well as language-wide APIs.)

Review: manual vs. automatic memory management

Allocation and freeing memory are critical concepts to know and be aware of; they form the basis of our mental-model of real machines. See Garbage collection strategies (Wikipedia). (Every CS major should know the basic ideas of garbage-collection, even though only our OS class actually covers it.)

If somebody asks “do you know C”, they're not asking about whether you can use if and struct and typedef; they are really asking if you understand the what is necessary whenever a string is allocated and de-allocated, passing values vs. references (memory-addresses), aliasing and dangling pointers, etc..

However, in practice, even top-notch programmers have difficulties getting manual memory-management correct.

upshot: Avoid manual de/allocation! Either use a language that does it automatically (like Java), or use a library for new/malloc/free which does it itself.


1 We use these to distinguish from actual pass-by-reference vs pass-by-value mechanisms, since Java actually uses only pass-by-value, but values are object-references (or primitives), and this leads to what we’re calling “reference semantics”. Likewise, Ada might optimize out parameters, and in parameters that aren’t mutated, by passing pointers under-the-hood, but it still has “value semantics” (or “copy semantics”).      
2

This is compounded, when objects contain references-to-other-objects. E.g.; a java.lang.String contains is characters in a "backing" character-array, so myName.charAt(3) incurs 3 trips to memory to get: the myName reference, the corresponding object (including a pointer-to-char[], the array/character itself. If you have a variable referencing an array of Person object-references each with an Address object reference which in turn has a String CityName which in turn has its backing char[], you're looking at 5 trips to memory to look that up. In Ada (think our example of array-of-Lines-containing-points-containing-Floats), a single memory access can suffice, since it's known at compile-time where to get People(7).address.cityName(3) lives.

Btw, it is notable that while Java 1 was originally slower than corresponding C programs by a factor of 2 or more, after a decade of work on javac, today Java programs are often (not always) competitive with C (within 0-10%).

     

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.