Review of Elementary Data Structures: Linked Lists

Review of Elementary Data Structures: Linked Lists
Slide Note
Embed
Share

Explore pointers, memory management, and linked lists in data structures. Understand the concepts through review materials, example code snippets, and reference resources. Enhance your knowledge of heap vs. stack memory, pointers and memory representation, and implementation of linked lists. Prepare for coding tasks involving lists and familiarize yourself with key concepts highlighted in the review slides.

  • Data Structures
  • Linked Lists
  • Pointers
  • Memory Management
  • Code Implementation

Uploaded on Mar 03, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Pointers and Dynamic Memory CMSC 202 and review for CMSC 341

  2. Representing Variables Regular variables int age; Array of ints int ages[4]; Struct with 2 data pieces struct Person { int age; char initial; }; Person p; Array of structs Person people[4]; int: age int[]: ages int: p.age char: p.initial Person: p Person[]: people

  3. Pointer Review int*: ptr Creating a pointer int* ptr; Connecting it to a pointee int a = 4; ptr = &a; Changing its pointee s value *ptr = 7; Changing pointees int b = 2; ptr = &b; int*: ptr int: a 4 int*: ptr int: a 7 int*: ptr int: a 7 int*: ptr int: b 2

  4. Pointer Operators & Examples: int a = 3; int* ptr = &a; *ptr = 8; Address of pointee Syntax: type* ptr = &variable; ptr = &variable2; * int b = 5; int* ptr2 = &b; ptr = ptr2; Dereferencing, Value of pointee Syntax: *ptr = value; variable = *ptr; = Assignment, point to something else Syntax: ptrA = ptrB; == Comparison Syntax: ptrA == ptrB int: a 3 8 int*: ptr

  5. Arrays and Pointer Arithmetic In C and C++ Arrays are simply a kind of pointer A pointer points to first item in collection Index into array is offset Examples int*: ptr int ages[4] = {0, 1, 2, 3}; int* ptr = &ages[2]; *ptr = 8; ptr++; *(ptr - 2) = 9; 0 1 9 2 8 3 int[]: ages ptr - 2

  6. Dynamic Memory and Classes Types of memory from Operating System Stack local variables and pass-by-value parameters are allocated here Heap dynamic memory is allocated here C malloc() memory allocation free() free memory C++ new create space for a new object (allocate) delete delete this object (free)

  7. New Objects new Works with primitives Works with class-types Syntax: type* ptrName = new type; type* ptrName = new type( params ); Constructor!

  8. New Examples Notice: These are unnamed objects! The only way we can get to them is through the pointer! int* intPtr = new int; int*: intPtr int: Pointers are the same size no matter how big the data is! Car* carPtr = new Car( Nissan , Pulsar ); Car*: carPtr Car: Nissan Pulsar Customer: Customer* custPtr = new Customer; Customer*: custPtr

  9. Deletion of Objects delete Called on the pointer to an object Works with primitives & class-types Syntax: delete ptrName; Example: delete intPtr; intPtr = NULL; Set to NULL so that you can use it later protect yourself from accidentally using that object! delete carPtr; carPtr = NULL; delete custPtr; custPtr = NULL;

  10. Video! Pointer Fun with Binky http://cslibrary.stanford.edu/104/

  11. Practice Assume you have a Shoe class: Create a pointer to a Shoe Connect the pointer to a new Shoe object Delete your Shoe object Set pointer to null Shoe* shoePtr; shoePtr = new Shoe; delete shoePtr; shoePtr = NULL;

  12. Dynamic Arrays?! Syntax type* arrayName = new type[ size ]; type* arrayName = new type[ size ] ( params ); delete [ ] arrayName; Example int* intPtr; intPtr = new int[ 5 ]; int*: intPtr Car* carPtr; carPtr = new Car[ 10 ]; Customer* custPtr; custPtr = new Customer[ 3 ]; Constructor!

  13. Dynamic Arrays?! Syntax type* arrayName = new type[ size ]; type* arrayName = new type[ size ] ( params ); delete [ ] arrayName; Example int* intPtr; intPtr = new int[ 5 ]; int*: intPtr Car* carPtr; carPtr = new Car[ 10 ] ( Nissan , Pulsar ); Customer* custPtr; custPtr = new Customer[ 3 ]; Constructor!

  14. Dynamic 2D Arrays char**: chArray2 Algorithm Allocate the number of rows For each row Allocate the columns Example const int ROWS = 3; const int COLUMNS = 4; char **chArray2; // allocate the rows chArray2 = new char* [ ROWS ]; // allocate the (pointer) elements for each row for (int row = 0; row < ROWS; row++ ) chArray2[ row ] = new char[ COLUMNS ];

  15. Dynamic 2D Arrays Delete? Reverse the creation algorithm For each row Delete the columns Delete the rows Example // delete the columns for (int row = 0; row < ROWS; row++) { delete [ ] chArray2[ row ]; chArray2[ row ] = NULL; } // delete the rows delete [ ] chArray2; chArray2 = NULL;

  16. 2D Vectors?! Notice the space, why?? Allocation vector< vector< int > > intArray; Deletion // allocate the rows intArray.resize ( ROWS ); // allocate the columns for (unsigned int i = 0; i < intArray.size( ); i++) intArray[ i ].resize( COLUMNS );

  17. Destructors Constructors Construct or create the object Called when you use new Destructors Destroy the object Called when you use delete Why is this needed? Dynamic memory WITHIN the class! Syntax: class ClassName { public: ClassName(); ~ClassName(); // other stuff }; // Constructor // Destructor

  18. Destructor Example class Car { public: Car( const string& make, int year); Car::Car( const string& make, int year) { m_make = new string(make); m_year = new int(year); } ~Car(); // Destructor Car::~Car() { delete m_make; m_make = NULL; private: string* m_make; int* m_year; }; // cleanup delete m_year; m_year = NULL; // cleanup }

  19. Dynamic Memory Rules Classes If dynamic data MUST have constructor MUST have destructor Delete After delete always set pointer to NULL Security For every new, there must be a delete. Otherwise, you risk memory leaks

  20. Practice Dynamically create an array of 50 Shoes Delete your array of shoes Clear the pointer Shoe* shoeArray = new Shoe[ 50 ]; delete shoeArray; shoeArray = NULL;

  21. Challenge Create a very simple Car class Dynamically allocate an array of Passengers within the car Create a constructor to allocate the array Create a deconstructor to delete the array

More Related Content