1 / 26

Engineering Problem Solving With C++ An Object Based Approach

Engineering Problem Solving With C++ An Object Based Approach. Chapter 9 Pointers and Creating Data Structures. Pointer Basics. A pointer is a variable that holds the memory address of another object

odell
Download Presentation

Engineering Problem Solving With C++ An Object Based Approach

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. Engineering Problem Solving With C++An Object Based Approach Chapter 9 Pointers and Creating Data Structures

  2. Pointer Basics • A pointer is a variable that holds the memory address of another object • If a variable p contains the address of another variable q, then p is said to point to q • If q is a variable at location 0x100 in memory, then p would have the value 0x100 (q’s address)

  3. Address Of Operator • The & operator is called the addressof operator. • The operator & in front of any variable produces the address of that variable. • Example: int x=75; cout << "x is " << x; cout << "\nthe addres of x is " << &x; 0x7fff8164 x 75 x is 75 the address of x is 0x7fff8164

  4. How to declare a pointer variable • pointer variables are declared using the indirection operator*, more commonly called the dereferencing operator. • general form - • type *variable_name, *variable_name; • type* variable_name; • when declaring more than one pointer variable, the * must precede each variable.

  5. Example int *iPtr; double *dPtr; • the variable iPtr is declared to be of type pointer to int • the variable dPtr is declared to be of type pointer to double • neither variable in this example has been initialized • declaring a pointer creates a variable capable of holding an address

  6. Example int *iPtr, i=6;char *s, str = "example";double *dPtr, d=1.25; iPtr s dPtr i str d 6 "example" 1.25

  7. NULL pointer • there is a literal value that can be assigned to any pointer • the literal value is NULL or 0 • in this context it is known as the nulladdressand does not point to anything which can be referenced. • trying to dereference the null address, results in a fatal error

  8. Example int *iPtr=0; char *s=NULL; //predefined constant in iostream.h double *dPtr=NULL; NULL iPtr s dPtr NULL NULL

  9. x xp ip Assigning values to a pointer • the assignment operator (=) is defined for pointers • the right operand of the assignment operator can be any expression that evaluates to the same type as the left Example: int x, *xp, *ip; xp = &x; ip = xp;

  10. The Base Type • In the declaration int *p; // the base type of p is int double *q; // the base type of q is double • Base type determines the size of an object to which the pointer is assumed to be pointing • The size of p and q are the same ( 4 bytes**), but p points to 4 bytes** and q points to 8 bytes** **compiler dependent

  11. Base Type continued • A pointer’s base type determines how the contents of the memory location pointed to by the pointer will be interpreted • The declaration: int *p; declares p to be a pointer to int. Whatever p points to will be interpreted as an int Base type also affects pointer arithmetic

  12. Assigning/Using Values • To assign a value using a pointer, use the * to dereference the pointer: int *iptr, intVal; iptr = &intVal; // assign iptr to addr intVal *iptr = 5; // make contents = 5 cout << *iptr << endl; // display contents cout << iptr << endl; // display address

  13. Comparing Pointers • You may compare pointers using relational operators • Common comparisons are: • check for null pointer (p == NULL) • Note: since NULL evaluates as false, and any other pointer evaluates as true, checking for a null pointer can be done as (!p) • check if two pointers are pointing to the same object (p == q) • Note: (*p == *q) means they are pointing to equivalent, but not necessarily the same data.

  14. Pointers and Arrays • The name of an array is the address of the first element (i.e. a pointer to the first element) • Arrays and pointers may often be used interchangeably Example int num[4] = {1,2,3,4}, *p; p = num; //same as p = &num[0]; cout << *p<<endl; p++; cout << *p; 1 2

  15. More Pointers and Arrays • You can also index a pointer using array notation Example: char myString[] = "This is a string"; char *str; str = myString; for(int i =0; str[i]; i++) //look for null cout << str[i]; What does this segment print? This is a string

  16. Arrays and Pointers • The name of an array is a pointer to the first element. However, the array name is not a pointer variable. It is a constantpointer that always points to the first element of the array and its value can not be changed. • Also when an array is declared, space for all elements in the array is allocated, however when a pointer variable is declared only space sufficient to hold an address is allocated

  17. Common Pointer Problems • Using uninitialized pointers int *iPtr; *iPtr = 100; iPtr has not been initialized. The value 100 will be assigned to some memory location. Which one determines the error. • Failing to reset a pointer after altering it’s value • Incorrect/unintended syntax

  18. Stack Heap Global data Program code Dynamic Allocation Using new and delete • Storage for data is allocated as needed from the free memory area known as the heap (or the freestore). • Run-time stack • Local variables • Formal parameters • Managed by the compiler • Heap • Dynamic storage • Managed by storage allocator

  19. Dynamic Memory Allocation • Dynamically allocated memory is determined at runtime • It allows a program to create as many or as few variables as required, offering greater flexibility • Dynamic allocation is often used to support data structures such as stacks, queues, linked lists and binary trees. • Dynamic memory is finite • Dynamically allocated memory may be freed during execution

  20. Operator new • Returns the address of memory allocated on the heap • If allocation fails, new terminates the program (or returns NULL on older compilers)

  21. Examples int *iPtr; iPtr = new int;// iPtr points to new // integer variable on heap double *dPtr; dPtr = new double[20]; // dPtr points to the first // double in anarray // of 20 doubles on heap

  22. Practice: int main() { int *iPtr, i; iPtr = new int; *iPtr = 7; double *dPtr; dPtr = new double[6]; cout << *iPtr << endl; for(i=0; i<6; i++) //SKETCH MEMORY dPtr[i] = 5; cout << endl; for(i=0; i<6; i++) cout << dPtr[i] << ' '; return 0; } 7 iPtr 5 5 5 5 5 5 dptr OUTPUT 7 5 5 5 5 5 5

  23. Operatordelete • The delete operator frees memory produced by new • Using delete to attempt to free any other type of address will produce problems • While delete uses a pointer variable, it does not destroy the pointer variable, it only frees the storage to which the variable is pointing.

  24. 100 delete Example int *ptr; ptr = new int (100); // can initialize, just like int cout << *ptr; //see if it worked delete ptr; //free the memory value of ptr is now undefined

  25. Example of dynamically allocated arrays double *dptr; const int size = 10; dptr = new double[size]; //allocate 10 doubles for(int I=0; I<size; I++) //read values into array cin >> dptr[I]; fun1(dptr, size); // pass array to fun1 delete [] dptr; //free all 10 elements prototype for fun1: void fun1(double*, int);

  26. Example using delete int *iPtr, *jPtr; iPtr = new int[3]; for (int i=0; i<3; ++i) iPtr[i] = i; for (int i=0; i<3; ++i) cout << iPtr[i] << endl; delete [] iPtr; jPtr = new int; *jPtr = 5; cout << *jPtr * 7 << endl; delete jPtr;

More Related