Download presentation
Presentation is loading. Please wait.
1
Dynamically allocating structures
2
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
3
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;
4
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
5
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; }
6
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
7
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
8
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
9
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
10
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
11
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;
12
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
13
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(…)
14
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
15
References [1] No references?
16
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 for more information.
17
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.