CMP 207 -Variables
CMP 207 -Variables
CSC 206
FOUNDATION OF SEQUENTIAL PROGRAMS
Assignment
Memory
Your programs /software has two main kinds of memory to
work with, which are:
• Stack memory
• Heap memory
The Picture
Stack Memory Heap Memory
String Object
s myChars
x
y He l l o
void toyCodeForMemory(int x)
{ int y = 10;
x += y;
String s = new String("Hello");
System.out.println(x + " " + y + s);
}
27
Stack memory
• Local variables for functions, whose size can be determined at call
time.
• Information saved at function call and restored at function return:
• Values of called arguments
• Register values:
• Return address (value of PC)
• Frame pointer (value of FP)
• Other registers
Heap memory
• Structures whose size varies dynamically (e.g. variable length arrays
or strings).
• Structures that are allocated dynamically (e.g. records in a linked list).
• Structures created by a function call that must survive after the call
returns.
Issues:
• Allocation and free space management
• Deallocation / garbage collection
Memory Allocation
• Before the assignment, the pointer may or may not point to a legitimate memory
• After the assignment, the pointer points to a legitimate memory.
What is a pointer?
In computer science, a pointer is a variable whose value is a location in the computer's
memory. For example If Mr Brown stands in a room and points to his friend Sele, then Mr
Brown is a pointer whose value is Sele’s location. A programmer must dereference the
pointer to retrieve the object it points to. Pointers do not take up much memory (RAM).
Copying a pointer to a large object is faster than copying the large object itself because only
the location needs to be copied, instead of the whole object.
Description of a pointer
Explicitly freeing memory in C++: the ‘delete’
Operator
The delete operator is used to free memory allocated with new operator
The delete operator should be called on a pointer to dynamically allocated
memory when it is no longer needed
How can one delete a single variable/object in an array
delete PointerName;
delete [] ArrayName;
After delete is called in a memory region, that region should no longer be
accessed by the program
The general convention is to set pointer of deleted memory to NULL
Any new must have a corresponding delete --- if not, the program has memory leak.
New and delete may not be in the same subroutine/function.
Explicit memory allocation/free in C (Works
also in C++)
• The malloc and free routines
• malloc is similar to new except that it specifies the exact memory size
• Return a (void *) -- needs to convert to the right pointer type
• free is equivalent to delete (only one form for both single item and many
items).
Why use dynamic memory allocation?
• Allows data (especially arrays) to take on variable sizes (e.g. ask the
user how many numbers to store, then generate an array of integers
exactly that size).
• Allows locally created variables to live past end of subroutine.
• Allows us to create many structures used in Data Structures and
Algorithms