140 likes | 245 Views
CSci 162 Lecture 14. Martin van Bommel. Pointers. Recall that pointers contain address of data ptr == &num *ptr == num == 10 Direct value of num is 10 Direct value of ptr is address of num Indirect value of ptr is 10. ptr. 10. num. Pointers as Parameters.
E N D
CSci 162Lecture 14 Martin van Bommel
Pointers • Recall that pointers contain address of data ptr == &num *ptr == num == 10 • Direct value of num is 10 • Direct value of ptr is address of num • Indirect value of ptr is 10 ptr 10 num
Pointers as Parameters • “Return” values from functions via pointers void swap ( int *x , int *y){ int tmp; tmp = *x; *x = *y; *y = tmp;} • Call using & (address of) operator swap(&a, &b);
Pointers and Arrays • Array function argument same as pointer void sum ( int array[]); • Same as void sum ( int *array ); • Call with array name passes address: sum ( list ); • Name of string (array of char) is an address cout << name;
Other Pointers • Pointer to structures customerType *customer; customer->id = 123; • Pointer to objects Rectangle *boxPtr; boxPtr->setWidth(10);
Memory Allocation • Static allocation • persists throughout program • e.g. global variables • Automatic allocation • allocated and freed during execution automatically • e.g. function variables • Dynamic allocation • allocated and freed by explicit statements during execution • e.g. dynamic variables
Dynamic Allocation • Program only occupies fraction of available storage when loaded into memory (static memory) • Rest of memory may be allocated as required • either automatically for functions (stack dynamic memory) • or explicitly by statements (dynamic memory) • Pool of unallocated memory available to a program is called the heap
new • C++ has function for allocating memory in heap • new – allocate block of memory of size of datatype that follows • e.g. to allocate space for an integer, use ptr = new int; • new returns the address of the block of storage • This address can be assigned to a pointer variable
Dynamic Memory Allocation • Declaring pointer - space in stack for address int *pnum;char *pchar;custT *pcust; pnum Stack pchar pcust • Need to allocate space for values • pnum = new int;pchar = new char;pcust = new custT; Heap
Detecting Errors in new • Heap may run out of space • If so, new returns pointer NULL • Programmer should check for this case • Usuallyptr = new int; if (ptr == NULL) { cout << ”Out of memory\n”; exit(0); }
Freeing memory • To help ensure you do not run out of memory, should free any storage possible • If finished with ptr, usedelete ptr; • Be careful though, because dereferencing ptrafter it has been freed will cause errors
Dynamic Arrays • To declare dynamic array: • Declare a pointer variable for the base of array • Call new to allocate memory for elements • Assign the result of new to pointer variable
Dynamic array example • To allocate a new 100-element integer array • First declare arr usingint *arr; • Then allocate storage usingarr = new int[100]; • Can refer to elements of array as usualarr[i] or *(arr + i) • To delete dynamic array, usedelete [] arr;
Dynamically allocated objects • Declaring pointers to objects does not actually create the objectRectangle *rectPtr; • Dynamically allocating objects creates the object and calls the constructorrectPtr = new Rectangle;or rectPtr = new Rectangle(5,10); • Deallocating object calls the destructordelete rectPtr;