1 / 14

CSci 162 Lecture 14

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.

hal
Download Presentation

CSci 162 Lecture 14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSci 162Lecture 14 Martin van Bommel

  2. 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

  3. 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);

  4. 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;

  5. Other Pointers • Pointer to structures customerType *customer; customer->id = 123; • Pointer to objects Rectangle *boxPtr; boxPtr->setWidth(10);

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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); }

  11. 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

  12. 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

  13. 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;

  14. 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;

More Related