Download presentation
Presentation is loading. Please wait.
Published byHillary Johns Modified over 9 years ago
1
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address. The type of item pointed to by a pointer variable is the target type.
2
Examples Declaring Pointers must declare a pointer as pointing to a value of a particular type. type *name; Pointer Declarations: int * ptr; char *cptr; double *dptr;
3
Pointers int * ptr; int v = 5; ptr = & v ; The & operator is called the address operator either of the following assignments will store the value of 10 in the variable v: v = 10 ; *ptr = 10 ;
4
Pointers: Dereferencing cont. So far the asterisk is used in two ways: 1. int *ptr; 2. *ptr = 10 ; the first declares the variable ptr as a pointer of type integer but it does not make it point to any memory address. the second assigns the value 10 to the memory location pointed at by ptr. dereferencing
5
Question Explain each line in the following code: int *ptr ; int number = 42 ; ptr = &number ; *ptr = 0 ; cout << *ptr <<endl ; cout << number << endl;
7
Pointer Graph Representation
8
Dereferencing
9
Pointers cont. Explain the following code: int *p1 ; int *p2; p1 = & count ; int *p2; *p2 = *p1; p2 = p1 ;
10
Quiz #3
11
Dynamic Variables Dynamic Variables (DVs) are different than normal variables in two aspects: 1. DVs are not declared (have no identifiers) 2. DVs are created during execution phase of a program and not during compilation, the keyword new is used for this purpose.
12
Dynamic Variables cont. Example: ptr = new int; this statement creates a DV of type integer and uses ptr to point to it. There is no identifier for the variable pointed at by ptr
13
Dynamic Variables cont. The creation of new DVs is called memory allocation and the allocated memory is called dynamic memory ptr = new int; Makes ptr point to a newly allocated integer variable from dynamic memory.
14
Dynamic Variables cont. int *ptr ; ptr = new int ; *ptr = 33;
15
Dynamic Arrays. To declare a dynamic array use: int *ptr ; ptr = new int[10] ; the new operator allocates a dynamic array of 10 integers and makes ptr point to its first element.
16
Dynamic Arrays cont. the statement: ptr[5] = 33; will store the value 33 as the 6 th element in the array pointed to by ptr
17
Heap When new allocates a dynamic variable or dynamic array, the memory comes from a location called the program’s heap (also called the free store). bad_alloc exception is thrown when new attempts to allocate memory and fails.
18
Question Determine what the following code will do size_t array_size ; int *numbers; cout << “how many numbers do you have?”; cin >> array_size ; numbers = new int[array_size] ;
19
Answer The operator new is used to dynamically allocate an array of size array_size that the user enters interactively.
20
The new operator & Objects throttle *t_ptr; t_ptr = new throttle(50) ; calls the throttle constructor with an integer argument
21
Question Who should initialize the components of a dynamically allocated array whose components are of a class data type ? Ans: the default constructor will initialize all components of the array
22
Delete Operator It is an efficient practice to release any heap memory that is no longer needed. The delete operator is used in C++ to release memory to the heap that is no longer needed.
23
Delete operation Examples: int *ptr; ptr = new int; … delete ptr ; int *p; p = new int[30]; … delete [] p;
24
Pointers as value parameters int *main_ptr; main_ptr = new int; make_it_42(main_ptr); void make_it_42(int* my_ptr) ; { *my_ptr = 42; }
25
Pointers as value parameters The following function prototype: void make_it_42(int* my_ptr) ; the int* indicates that the parameter is of data type integer pointer. the parameter is a value parameter because of the absence of the & operator.
26
Array Parameters void make_it_all_42( double data[], size_t n); …. double *numbers; numbers = new double[10]; make_it_all_42(numbers, 10); …. void make_it_all_42(double data[], size_t n) { for(size_t i=0; i<n; i++) data[i] = 42; }
27
void make_it_all_42(double * data, size_t n) { for(size_t i=0; i<n; i++) data[i] = 42; } void make_it_all_42(double data[], size_t n) { for(size_t i=0; i<n; i++) data[i] = 42; }
28
Array parameters cont. A parameter that is a pointer or array may include the const keyword. No changes to the actual parameter or the array are possible in this case bool is_42(const int* my_ptr); double average(const double data[], … )
29
Pointers Reference Parameters Sometimes a function needs to change a pointer parameter so that the pointer points to a new location. Example: void alloc_doubles(double*& ptr, size_t & n);
30
Pointer Reference Parameters void alloc_doubles(double*& ptr, size_t & n); … double *numbers; size_t array_size; alloc_doubles(numbers, array_size); … void alloc_doubles(double*& ptr, size_t & n) { cin>> n; ptr = new double[n]; }
31
The Bag with dynamic array The constructor of the dynamic Bag should allocate the dynamic array that the member variable data points to. How big the initial array size should be? that may be determined by a parameter that is passed to the constructor The size of the bag will increase as needed.
32
The bag class class bag { public: ……. private: value_type *data ; // dynamic array ptr size_type used ; // how much being used size_type capacity; // current bag capacity };
33
Invariant of the dynamic bag the number of items in the bag is in the member variable used The items are stored in a partially filled dynamic array that is pointed to by data The total size of the dynamic array is in the member variable capacity
34
Question Which function/operator are used to increase the capacity of the bag ? Ans: insert and the operator += What will happen if the initial bag size is two small ? Ans: too many increases to memry by inset or += will slow down the execution
35
How capacity is increased larger memory block is allocated (new) items from completely used memory block (old memory) are copied to the newly allocated memory the old memory is released
36
New Bag Constructor bag(size_type init_capacity = DEF_CAP); // postcondition: the bag is empty with a // capacity given by the parameter. The insert // function will work efficiently ( without // allocating new memory ) until this // capacity is reached. Example call: bag kilosack(1000);
37
The reserve function A member function that is used to adjust a bag’s capacity void reserve(size_type new_capacity) // postcondition: the bag’s current capacity // is changed to new_capacity. The insert // function will work efficiently ( without // allocating new memory) until newly // allocated, larger, capacity is reached
38
Question Name five member functions that allocate dynamic memory for the bag ? Ans: constructor, reserve, insert, += and +
39
Pointer Arithmetic The only legal arithmetic operators on pointers are adding or subtracting an integer, or subtracting one pointer from another.
40
Pointer Arithmetic In C++, pointer arithmetic is automatically done in units of the pointer's underlying base type. Adding 1 to a pointer to an array element gives a pointer to the next element - regardless of whether we have an array of ints, an array of doubles, or an array of any other type.
41
Pointer Arithmetic ar + i is a pionter to the ith element beyond ar &ar[i] is equivalent to ar + i ar[i]is equivalent to*(ar + i)
42
What is the output? int main() { int * array; array = new int[10]; *array = 33; *(array + 3) = 14; cout<<array[0]<<array[3]; return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.