Dynamic Memory Allocation in C++
An Introduction |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
new
operator is used to dynamically allocate
memory
*a
and a[0]
are
syntactically different ways to refer to the same value
[]
operator performs pointer arithemtic
and de-references the resulting pointer.new
:
delete
operator in C++delete
operator does not delete the
pointer, it takes the memory being pointed to
and returns it to the heapdelete
operator is needednew
operator but not returned to
the heap using the delete
operator
this->
~
character)delete
operator is applied to
a pointer to an object the object's destructor is
calledw
is a pointer to the array
w
is a Weight
object (not a reference to a Weight
)
Weight
is constructed using the
default constructor
main
main
that is passed command-line argumentsint main(int argc, const char *argv[])