Dynamically allocating structures

Slides:



Advertisements
Similar presentations
Recursive binary search
Advertisements

For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
Pointers.
Reference variables, pass-by-reference and return-by-reference
Dynamically allocating arrays
Anatomy of a program.
Binary search.
Command-line arguments
Pointer arithmetic.
Console input.
Dangling pointers.
This.
Sorted arrays.
Dynamically allocating arrays within structures
Dynamic memory allocation
Break statements.
Linked Lists.
Wild pointers.
The comma as a separator and as an operator
Bucket sort.
The ternary conditional operator
Memory leaks.
Pushing at the back.
Bit-wise and bit-shift operators
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.
Anatomy of a program.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Pointer arithmetic.
Class variables and class functions
Operator overloading.
The std::string class.
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.
Searching and sorting arrays
Protecting pointers.
Data structures: class
An array class: constructor and destructor
Constructors.
This.
Recursive binary search
Presentation transcript:

Dynamically allocating structures

Outline In this lesson, we will: Learn how to dynamically allocate structures Learn how to deallocate dynamically allocated structures Learn how to access member variables of dynamically allocated structures Dot and arrow operators

Recall structures A structure contains multiple member variables Our 3-d vector is an example of that struct vector_3d_t; // Structure declaration struct vector_3d_t { // Structure definition double x; double y; double z; }; A structure could contain member variables of different types struct array_t; // Structure declaration struct array_t { // Structure definition int data[4]; std::size_t capacity;

Recall structures Creating a local variable of a structure and accessing member variables using the dot operator int main() { vector_3d_t position{1.0,2.0,3.0}; std::cout << position.x << std::endl; std::cout << position.y << std::endl; std::cout << position.z << std::endl; return 0; } Output: 1 2 3

Dynamically allocating structures A structure can be dynamically allocated and then deallocated int main() { vector_3d_t *p_position{new vector_3d_t { 1.0, 2.0, 3.0}}; // Use p_position here … delete p_position; p_position = nullptr; return 0; }

Dynamically allocating structures Access member variables and print them to console int main() { vector_3d_t *p_position{new vector_3d_t { 1.0, 2.0, 3.0}}; std::cout << (*p_position).x << std::endl; std::cout << (*p_position).y << std::endl; std::cout << (*p_position).z << std::endl; delete p_position; p_position = nullptr; return 0; } Output: 1 2 3

Dynamically allocating structures To access the value of a member variable Dereference pointer first, then access the appropriate member variable The parenthesis are necessary (*p_position).x C++ provides a different way to perform the same access using the arrow operator (->) with pointers The arrow operator is a minus followed by a greater than character It is a dereference operator exclusively used with pointers to structures that have member variables p_position->x

Dynamically allocating structures We can rework the example with the arrow operator int main() { vector_3d_t *p_position{new vector_3d_t {1.0, 2.0, 3.0}}; std::cout << p_position->x << std::endl; std::cout << p_position->y << std::endl; std::cout << p_position->z << std::endl; delete p_position; p_position = nullptr; return 0; } Output: 1 2 3

Dynamically allocating structures Assignments to member variables can also be made by accessing the member variable using the arrow operator int main() { vector_3d_t *p_position{new vector_3d_t {1.0, 2.0, 3.0}}; p_position->x = 10; std::cout << p_position->x << std::endl; std::cout << p_position->y << std::endl; std::cout << p_position->z << std::endl; delete p_position; p_position = nullptr; return 0; } Output: 10 2 3

Passing pointers to structures as parameters Pointers to structures can be passed as parameters to functions void print( vector_3d_t *v ) { std::cout << v->x << "," << v->y << "," << v->z << std::endl; } int main() { vector_3d_t *p_position{new vector_3d_t {1.0, 2.0, 3.0}}; print( p_position ); p_position->x = 10; delete p_position; p_position = nullptr; return 0; Output: 1,2,3 10,2,3

Passing pointers to structures as parameters Pointers to structures can also be returned double norm( vector_3d_t v ) { return std::sqrt( v.x*v.x + v.y*v.y + v.z*v.z ); } vector_3d_t *larger_norm( vector_3d_t *v, vector_3d_t *u) { vector_3d_t * big_norm{nullptr}; if ( norm(*v) > norm(*u) ) { big_norm = v; } else { big_norm = u; return big_norm;

Passing pointers to structures as parameters Pointers to structures can also be returned int main() { vector_3d_t *p_pos1{new vector_3d_t{1.0,2.0,3.0}}; vector_3d_t *p_pos2{new vector_3d_t{11.0,22.0,33.0}}; print( p_pos1 ); print( p_pos2 ); print( larger_norm( p_pos1, p_pos2 ) ); delete p_pos1; p_pos1 = nullptr; delete p_pos2; p_pos2 = nullptr; return 0; } Output: 1,2,3 11,22,33

Quick review What is the difference in using the dot operator versus the arrow operator when accessing member variables of a struct? For the following structure: struct array_t { int data[capacity]; std::size_t capacity; }; Write a function that dynamically allocates an instance of array_t, takes in values to initialize the data member variable as a pointer to an array, its capacity, and returns a pointer to it. Call the function memory_alloc_struct(…)

Summary Following this lesson, you now Know how to dynamically allocate structures Understand how to deallocate dynamically allocated structures Understand how to access member variables of structures using the dot and arrow operators

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.