1 / 16

Pointers and Dynamic Arrays in Chapter 9

This chapter covers the basics of pointers and dynamic arrays in C++. Topics include pointer variables, pointer operators, dynamic instantiation, NULL pointers, basic pointer manipulations, memory management, and typedef.

ralvarado
Télécharger la présentation

Pointers and Dynamic Arrays in Chapter 9

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. Chapter 9 Pointers and Dynamic Arrays (9.1)

  2. Pointers • A variables which holds the memory address for a variable of a specific type. • Call-by-Reference is actually using pointer.

  3. Pointer Variables • Declare a pointer: type *name; Ex: int *p; // declares *p as an integer and p as the address of that integer. // Therefore p = &(*p) • Multiple declaration: type *name1, *name2; Ex: int *p, *q; // p and q are pointers • Mixed declaration: Ex: int *p, q, *r; // p and r are pointers, q is an integer • Pointers can be initialized: Ex: int a, *p = &a; // means pointer p=&a, NOT *p=&a int *p = &a, a; // NO! a is still not declared at time of assigning • Pointer must be declared as pointer to a specific type. Can’t have integer pointer points to double pointer.

  4. Pointer Operators • * (not in the declaration): • When used in front of a pointer variable produces the value of the variable it points to. • Called dereferencing operator. • &: • When used in front of a ordinary variable produces the address of that variable. (or a pointer that points to the variable) • =: • Assign a pointer to the address of another variable of the same type • Assign a pointer to another pointer int *p, q, *r; r = &q; // q must declared before r so the address for q exists p = r; // r already points to an existing address. • Assign content of a pointer to content of another pointer of the same type *p = *q; • Can’t directly assign a pointer to an integer. It will point to an absolute address in memory. Too dangerous. It can overwrite the value at that specific memory location used by other programs. p = 12345; • Can’t directly assign a pointer to an expression (no address). It will point to an absolute address in memory. Too dangerous. It can overwrite the value at that specific memory location used by other programs. p = &(q+1);

  5. The new operation • Allows creating a new pointer variables during execution. • Called dynamic instantiation. • The variable is then called dynamic variable. int *p; p = new int; *p = 100; p ? p ? p 100

  6. The NULL pointer • defined in cstddef.h • Global and static pointers are initialized to zero • Cannot be dereferenced • If no memory is available when the new command is executed, a NULL value (or std::badalloc exception) is returned

  7. Basic pointer Manipulations int *p, q=20, *r; // p and r are pointers, q is an integer p = new int; *p = 42; r = p; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; p ? q 20 r ? p ? q 20 r ? p 42 q 20 r ? p 42 q 20 r *p == 42 *r == 42

  8. Basic pointer Manipulations (cont.) *r = 52; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; *r = *p + 7; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; p = new int; p 52 q 20 r *p == 52 *r == 52 p 59 q 20 r *p == 59 *r == 59 p ? q 20 r 59

  9. Basic pointer Manipulations (cont.) *p = 88; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; r = &q; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; • What is the value of *r if q is changed to 40? p 88 q 20 r 59 *p == 88 *r == 59 p 88 r 59 20 q *p == 88 *r == 20 40

  10. Basic Memory Management(Heap or Freestore) • A special area of free memory reserved for dynamic variables. • When a pointer is created (with new operator), it gets an address from the heap. • If the heap is empty, a call to new return a NULL pointer. Then, the call to new fails.

  11. Basic Memory Management(delete operator) • When the memory the pointer points to is no longer needed, you must use delete operator to return freed memory to the heap. The memory then can be reused for creating new dynamic variables. • If the pointer variable is out of scope, the memory used by undeleted pointer will be still occupied. It causes memory leak.

  12. Basic Memory Management(dangling pointers) • When the delete operator is applied to a pointer variable, the dynamic variable it is point to is destroyed. • Then, the value of the pointer variable is undefined. • And the pointer becomes dangling pointer. • Any pointer point to the same dynamic variable also becomes a dangling pointer. • So, remember set the pointer to NULL.

  13. Basic pointer Manipulations (dangling pointers) int *p, *q; p = new int; *p = 10; p = q; delete p; p = NULL; q = NULL; p ? p p ? 10 q ? q ? p ? p NULL q ? q ? p 10 NULL p q ? q p 10 q

  14. Type define (typedef) • Allows you to give a name to a type for clarity. • Ex: typedef int* IntPtr; IntPtr pointer1, pointer2;

  15. Static Variables • Static variables: Keyword staticin front of variable in a function makes its viability global but its visibility local. Initialized on first function call, it maintains it’s value through additional calls until the program ends. void tally() { static int count = 0; cout << ++count << endl; } void main () { for (int i=0; i<5; i++) tally(); } 1 2 3 4

  16. Dynamic and Automatic Variables • Dynamic: created and destroyed by the programmer. (pointers) • Automatic: automatically created on entrance to block and destroyed on exit. (Ordinary variables) • Global: Outside of main. Can be seen by any other file or program.

More Related