Memory Management
Java : background memory management
Advantage: allowing programmer to concentrate on more application specific details
Disadvantage: programmer has little control over how memory management is performed.
C++ : explicit memory management
Advantage:permitting much more efficient use of memory
Can bloat a program at run‑time, or make a program very inefficient
Common errors in c++
Using a value before it has been initialized.
Allocating memory for a value, then not deleting it once it is no longer being used.
Using a value after it has been freed.
Memory model
C++ model is much closer to the actual machine representation than is Java memory model.
Permits an efficient implementation at the expense of needing increased diligence on the part of the programmer.
Stack‑resident – created automatically when a procedure is entered or exited.
Heap‑resident – must be explicitly created using the new operator.
Stack Resident Memory Values
C++
Both the stack and the heap are internal data structures managed by a run‑time system.
Values on a stack are strongly tied to procedure entry and exit.
When a procedure begins execution, a new section of the stack is created.
Java
only primitive values are truly stack resident.
objects and arrays are always allocated on the heap.
Programm test
void test () // Java
{ int i; int a[ ] = new int [10]; anObject ao = new anObject(); ….. } |
void test () // C++
{ int i, int a [10]; anOb j e ct ao; ….. } ¬ |