Pointers as arguments and return values

Slides:



Advertisements
Similar presentations
Throwing and catching exceptions
Advertisements

Recursive binary search
For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
Pointers.
Dynamically allocating arrays
Anatomy of a program.
Switch statement.
Binary search.
Command-line arguments
Throwing exceptions.
Pointer arithmetic.
Console input.
Dangling pointers.
This.
Floating-point primitive data types
Dynamically allocating arrays within structures
Dynamic memory allocation
Break statements.
Linked Lists.
Wild pointers.
The comma as a separator and as an operator
Selection sort.
Bucket sort.
The ternary conditional operator
Dynamically allocating structures
Memory leaks.
Pushing at the back.
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Templated Linked Lists
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Dynamically allocating arrays
Code-development strategies
Throwing exceptions.
Selection sort.
Insertion sort.
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Pointer arithmetic.
Class variables and class functions
Operator overloading.
Dynamic allocation of arrays
Templates.
This.
Dynamic memory allocation
Insertion sort.
Sorted arrays.
Sorting algorithms.
Issues with classes.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Selection sort.
Searching and sorting arrays
Protecting pointers.
Data structures: class
An array class: constructor and destructor
This.
Recursive binary search
Presentation transcript:

Pointers as arguments and return values

Outline In this lesson, we will: Pass pointers to local variables as parameters to functions Pass pointers to arrays as parameters to functions Pass pointer references as parameters to functions Return pointers from functions

Pointers to local variables as parameters A function can take parameters that are of a pointer type void increment( int *p_x ) { *p_x = *p_x + 1; } int main() { int *p_int{new int{42}}; std::cout << "p_int: " << *p_int << std::endl; increment( p_int ); delete p_int; p_int = nullptr; return 0; Output: p_int: 42 p_int: 43

Pointers to local variables as parameters A function can take multiple parameters that are of a pointer type void divide( double *p_y, int *p_x ) { if (*p_x != 0) { *p_y /= *p_x; } int main() { int *p_int{new int{42}}; double *p_db{new double{22.22}}; std::cout << "p_db: " << *p_db << std::endl; divide( p_db, p_int ); // deallocate p_int and p_db return 0; Output: p_db: 22.22 p_db: 0.529048

Pointers to local variables as parameters Pointers that are passed-by-value void swap( int *p_x, int *p_y ) { int *p_temp = p_x; p_x = p_y; p_y = p_temp; } int main() { int *p_int1{new int{42}}; int *p_int2{new int{33}}; std::cout << p_int1 << ", " << p_int2 << std::endl; swap( p_int1, p_int2 ); delete p_int1; p_int1 = nullptr; delete p_int2; p_int2 = nullptr; Output: 0x18c7c20, 0x18c7c40

Pointers to local variables as parameters Pointers that are passed-by-reference void swap( int *&p_x, int *&p_y ) { int *p_temp = p_x; p_x = p_y; p_y = p_temp; } int main() { int *p_int1{new int{42}}; int *p_int2{new int{33}}; std::cout << p_int1 << ", " << p_int2 << std::endl; swap( p_int1, p_int2 ); delete p_int1; p_int1 = nullptr; delete p_int2; p_int2 = nullptr; Output: 0x19ebc20, 0x19ebc40 0x19ebc40, 0x19ebc20

Pointers as parameters Recall that the address of the first element of the array is passed when passing an array as a parameter int sum( int data[], std::size_t capacity ) { int sum{0}; for ( std::size_t i{0}; i < capacity; ++i) { sum += data[i]; } return sum;

Pointers to arrays as parameters A pointer can be used to pass the address of an element of an array int sum( int *a_data, std::size_t capacity ) { int sum{0}; for ( std::size_t i{0}; i < capacity; ++i ) { sum += a_data[i]; } return sum; int main() { std::size_t capacity{5}; int data[capacity]{1,2,3,4,5}; int *a_my_data{&data[0]}; std::cout << sum( a_my_data, capacity ) << std::endl; return 0; }

Pointers to arrays as parameters A pointer can be used to pass the address of the starting element of a dynamically allocated array Must ensure to deallocate int sum( int *a_data, std::size_t capacity ) { int sum{0}; for (std::size_t i{0}; i < capacity; ++i) { sum += a_data[i]; } return sum; int main() { std::size_t capacity{5}; int *a_my_data{new int [capacity]{1, 2,3,4,5}}; std::cout << sum( a_my_data, capacity ) << std::endl; delete [] a_my_data; a_my_data = nullptr; return 0; }

Returning pointers A pointer can be returned from a function int *create_dyn_array( int data[], std::size_t capacity) { int *a_darray{new (std::nothrow) int[capacity]}; // Check a_darray for being nullptr for (std::size_t i{0}; i < capacity; ++i) { a_darray[i] = data[i]; } return a_darray; A pointer to a dynamically created array is returned It must be deallocated later

Returning pointers A pointer can be returned from a function int *create_dyn_array( int data[], std::size_t capacity) { int *a_darray{new (std::nothrow) int[capacity]}; // Check a_darray for being nullptr for (std::size_t i{0}; i < capacity; ++i) { a_darray[i] = data[i]; } return a_darray; A pointer to a dynamically created array is returned It must be deallocated later Good to check if it is nullptr int *a_array{create_dyn_array( data_array, capacity )}; if ( a_array == nullptr ) { // Handle this situation. }

Returning pointers Alternatively, you can catch exceptions of type std::bad_alloc that may be raised try { int * a_array{create_dyn_array( data_array, capacity )}; // Do something with the array } catch ( std::bad_alloc e ) { std::cout << “Error: memory not allocated.” << std::endl; // Handle this situation. }

Alternative to returning pointers A pointer could also be passed as a parameter that is changed instead of returning a pointer void create_dyn_array( int data[], int *&a_darray, std::size_t capacity) { a_darray = new (std::nothrow) int[capacity]; // Check a_darray for being nullptr for (std::size_t i{0}; i < capacity; ++i) { a_darray[i] = data[i]; } A pointer to a dynamically created array can be accessed using a_darray

Quick review Write a function that deallocates a dynamically allocated array void deallocate_array (…) { // Write the code } Can you pass pointer pass-by-reference to change the contents of the memory pointed to by the pointer?

Summary Following this lesson, you now Understand passing pointers as parameters to functions Pointers passed-by-value Pointers passed-by-reference Returning pointers from functions

References [1] No references?

Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see https://www.rbg.ca/ for more information.

Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.