• Nie Znaleziono Wyników

Programming and Data Structures Programming and Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "Programming and Data Structures Programming and Data Structures"

Copied!
48
0
0

Pełen tekst

(1)

Programming and Data Structures Programming and Data Structures

Lecture 4 Lecture 4

Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>

Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>

(2)

Pointers Pointers

int* pointer; //pointer points to int variable

allows access to any memory location (pointer variables are of a fixed size depending only on the processor architecture, e.g. 32-bit, 64-bit)

a pointer type variable contains the address of the variable it points to (random by default):

empty address initialization NULL (defined in cstdlib) or nullptr (keyword since c++11)

pointer = nullptr;

initialization with the address of an existing automatic variable (unsafe)

int variable;

pointer = &variable; //reference operator

initialization with the address of a dynamic variable

pointer = new int; //memory allocation delete pointer; //memory deallocation

access to the pointed variable only for non-empty pointers

p v

(3)

Pointers Pointers

the pointer variable assignment operator copies only addresses (not the values of the pointer variables)

int* p1 = new int;

int* p2;

p2 = p1; //p2 points to the same as p1

sharing the address of one variable will lead to the creation of the so-called dangling pointer after freeing memory occupied by this variable

delete p1;

cout << *p2; //undefined behaviour

before freeing the memory for the variable pointed by the pointer, change the values for all pointers pointing to this variable except for one (so-called

reference counting)

p2 = nullptr; //p2 no longer points to the common variable //p1 is the only pointer to this variable delete p1; //memory deallocation is now safe

p1 p2

p1 p2

?

(4)

Pointers to structures Pointers to structures

access to structures pointed (only for non-empty pointers):

dereference and then selection (* and .): (*pointer).member

pointer dereference (->): pointer->member

class Student { string name;

int index;

public:

Student(string _name);

int getName() const;

… //other methods };

Student* p = new Student(”Scott Tiger”); //allocation cout << (*p).getName(); //dereference and selection cout << p->getName(); //pointer selection

delete p; //deallocation

(5)

Pointer «this»

Pointer «this»

pointer to the object for which the method was called (available in any structure / class method)

class Vector { double* v;

int dim;

public:

Vector(int dim) { v = new double[this->dim = dim]; } Vector& operator=(const Vector&);

};

Vector& Vector::operator=(const Vector &r) { if(this != &r) { //address check

delete[] v;

v = new double[dim = r.dim];

for(int i = 0; i < dim; i++) v[i] = r.v[i];

} return *this; //dereference of «this» returns the object

(6)

Pointer safety Pointer safety

pointer dereference is only possible when the pointer is not empty

(conditional use of dereference, pointer voidness usually means a special case in the algorithm)

the name of the pointer should clearly define its application (simple inference when the pointer is empty)

appointing dedicated pointers for memory allocation and deallocation

4 steps of correct memory deallocation (preventing the formation of dangling pointers):

(1) attaching the pointer and assigning the address of object to be released (2) optional protection of important data from the object

(3) detaching (modification) of all pointers that store the address of the object (except the pointer from the first step)

(7)

Linked list Linked list

allows for noncontiguous memory allocation when implementing ADT

composed of nodes connected by pointers - each node contains data and address of the next (single-linked) and the previous one (double-linked)

advantages: high scalability and extensibility, optimal memory consumption, easy addition and removal of data without the need to change the memory location of other data

disadvantages: implementation (operations on pointers), low read

performance (no direct access to any element), additional memory required to remember node addresses

A next

B next

C head

(8)

ADT list implementation ADT list implementation

allows for an unlimited size of the structure

a pointer to the first (optionally also the last) node of the linked list

basic methods:

parameterless constructor (empty pointers, no nodes)

destructor (memory deallocation of all nodes)

copy constructor (memory allocation for nodes)

assignment operator (old memory deallocation and new allocation for nodes)

insertion (memory allocation for a new node)

removal (memory deallocation for the node removed)

(9)

Linked lists – critical methods Linked lists – critical methods

due to the organization of noncontiguous memory (presence of pointer fields), the following methods must be implemented for the linked list:

destruction / clearing of the list (multiple removal of the first node until the list becomes empty) – the default destructor does not free memory

copy construction (allocation of as many new nodes as there are in the source list with data copying) – the default copy constructor copies only the address of the first/last node (so-called shallow copy with sharing nodes between lists)

assignment of lists (deallocation of redundant nodes / allocation of additional nodes with copying data from the source list) - the default assignment operator only copies the address of the first/last node, losing access to the previous list (shallow copy, memory leak)

(10)

Insertion at the beginning Insertion at the beginning

A next

B next

C head

(1) create a new node (creator) with the given value and the address of the first node (head) as the address of the next (next)

(11)

Insertion at the beginning Insertion at the beginning

(1) create a new node (creator) with the given value and the address of the first node (head) as the address of the next (next)

A next

B next

C head

creator V next

(12)

Insertion at the beginning Insertion at the beginning

A next

B next

C head

creator V next

(1) create a new node (creator) with the given value and the address of the first node (head) as the address of the next (next)

(2) set the first node pointer (head) to the new node

(3) if there is a pointer to the last node (tail) and it points to an empty address, set it to the new node

(13)

Insertion at the beginning Insertion at the beginning

A next

B next

C head

creator V next

(1) create a new node (creator) with the given value and the address of the first node (head) as the address of the next (next)

(2) set the first node pointer (head) to the new node

(3) if there is a pointer to the last node (tail) and it points to an empty address, set it to the new node

(14)

Insertion at the beginning Insertion at the beginning

(1) create a new node (creator) with the given value and the address of the first node (head) as the address of the next (next)

(2) set the first node pointer (head) to the new node

(3) if there is a pointer to the last node (tail) and it points to an empty address, set it to the new node

A next

B next

C head

V next

(15)

Removal at the beginning Removal at the beginning

A next

B next

C head

(1) if the list is empty throw an exception and abort (2) set a dedicated pointer (killer) to the first node

(16)

Removal at the beginning Removal at the beginning

A next

B next

C head

killer

(1) if the list is empty throw an exception and abort (2) set a dedicated pointer (killer) to the first node

(17)

Removal at the beginning Removal at the beginning

A next

B next

C head

killer

(1) if the list is empty throw an exception and abort (2) set a dedicated pointer (killer) to the first node

(3) set the pointer of the first node (head) to the second node (using the address in the pointer next from the first node)

(4) if there is a pointer to the last node (tail) and it points to the one to be removed, set it to an empty address

(18)

Removal at the beginning Removal at the beginning

A next

B next

C head

killer

(1) if the list is empty throw an exception and abort (2) set a dedicated pointer (killer) to the first node

(3) set the pointer of the first node (head) to the second node (using the address in the pointer next from the first node)

(4) if there is a pointer to the last node (tail) and it points to the one to be removed, set it to an empty address

(19)

Removal at the beginning Removal at the beginning

A next

B next

C head

killer

(1) if the list is empty throw an exception and abort (2) set a dedicated pointer (killer) to the first node

(3) set the pointer of the first node (head) to the second node (using the address in the pointer next from the first node)

(4) if there is a pointer to the last node (tail) and it points to the one to be removed, set it to an empty address

(5) release the node pointed by the dedicated pointer (killer)

(20)

Removal at the beginning Removal at the beginning

B next

C head

killer

(1) if the list is empty throw an exception and abort (2) set a dedicated pointer (killer) to the first node

(3) set the pointer of the first node (head) to the second node (using the address in the pointer next from the first node)

(4) if there is a pointer to the last node (tail) and it points to the one to be removed, set it to an empty address

(5) release the node pointed by the dedicated pointer (killer)

(21)

Removal at the beginning Removal at the beginning

(1) if the list is empty throw an exception and abort (2) set a dedicated pointer (killer) to the first node

(3) set the pointer of the first node (head) to the second node (using the address in the pointer next from the first node)

(4) if there is a pointer to the last node (tail) and it points to the one to be removed, set it to an empty address

(5) release the node pointed by the dedicated pointer (killer)

B next

C head

(22)

Insertion at the end (ver. 1) Insertion at the end (ver. 1)

A next

B next

C head

version with a pointer to the first node only (head):

(1) move the auxiliary pointer (tmp) from the beginning of the list to the last node

(23)

Insertion at the end (ver. 1) Insertion at the end (ver. 1)

A next

B next

C head

version with a pointer to the first node only (head):

(1) move the auxiliary pointer (tmp) from the beginning of the list to the last node

(24)

Insertion at the end (ver. 1) Insertion at the end (ver. 1)

A next

B next

C head

version with a pointer to the first node only (head):

(1) move the auxiliary pointer (tmp) from the beginning of the list to the last node

(25)

Insertion at the end (ver. 1) Insertion at the end (ver. 1)

A next

B next

C head

version with a pointer to the first node only (head):

(1) move the auxiliary pointer (tmp) from the beginning of the list to the last node (2) create a new node (creator) with the given value and aan empty value as the

address of the next (next)

(26)

Insertion at the end (ver. 1) Insertion at the end (ver. 1)

A next

B next

C head

V

version with a pointer to the first node only (head):

(1) move the auxiliary pointer (tmp) from the beginning of the list to the last node (2) create a new node (creator) with the given value and aan empty value as the

address of the next (next)

(27)

Insertion at the end (ver. 1) Insertion at the end (ver. 1)

A next

B next

C head

V

version with a pointer to the first node only (head):

(1) move the auxiliary pointer (tmp) from the beginning of the list to the last node (2) create a new node (creator) with the given value and aan empty value as the

address of the next (next)

(3) move the next pointer at the node pointed to by tmp (or the head pointer when tmp is empty) to the new node

(28)

Insertion at the end (ver. 1) Insertion at the end (ver. 1)

A next

B next

C head

next V

version with a pointer to the first node only (head):

(1) move the auxiliary pointer (tmp) from the beginning of the list to the last node (2) create a new node (creator) with the given value and aan empty value as the

address of the next (next)

(3) move the next pointer at the node pointed to by tmp (or the head pointer when tmp is empty) to the new node

(29)

Insertion at the end (ver. 1) Insertion at the end (ver. 1)

version with a pointer to the first node only (head):

(1) move the auxiliary pointer (tmp) from the beginning of the list to the last node (2) create a new node (creator) with the given value and aan empty value as the

address of the next (next)

(3) move the next pointer at the node pointed to by tmp (or the head pointer when tmp is empty) to the new node

A next

B next

C head

next V

(30)

Insertion at the end (ver. 2) Insertion at the end (ver. 2)

A next

B next

C

head tail

version with pointers to the first and the last node (head and tail):

(1) create a new node (creator) with the given value and an empty value as the address of the next (next)

(31)

Insertion at the end (ver. 2) Insertion at the end (ver. 2)

A next

B next

C head

V

tail

version with pointers to the first and the last node (head and tail):

(1) create a new node (creator) with the given value and an empty value as the address of the next (next)

(32)

Insertion at the end (ver. 2) Insertion at the end (ver. 2)

A next

B next

C head

V

tail

version with pointers to the first and the last node (head and tail):

(1) create a new node (creator) with the given value and an empty value as the address of the next (next)

(2) move the next pointer in the last node pointed to by tail (or the head pointer when tail is empty) to the new node

(33)

Insertion at the end (ver. 2) Insertion at the end (ver. 2)

A next

B next

C head

next V

tail

version with pointers to the first and the last node (head and tail):

(1) create a new node (creator) with the given value and an empty value as the address of the next (next)

(2) move the next pointer in the last node pointed to by tail (or the head pointer when tail is empty) to the new node

(34)

Insertion at the end (ver. 2) Insertion at the end (ver. 2)

A next

B next

C head

next V

tail

version with pointers to the first and the last node (head and tail):

(1) create a new node (creator) with the given value and an empty value as the address of the next (next)

(2) move the next pointer in the last node pointed to by tail (or the head pointer when tail is empty) to the new node

(3) set the tail pointer to the new node

(35)

Insertion at the end (ver. 2) Insertion at the end (ver. 2)

A next

B next

C head

next V

tail

version with pointers to the first and the last node (head and tail):

(1) create a new node (creator) with the given value and an empty value as the address of the next (next)

(2) move the next pointer in the last node pointed to by tail (or the head pointer when tail is empty) to the new node

(3) set the tail pointer to the new node

(36)

Insertion at the end (ver. 2) Insertion at the end (ver. 2)

version with pointers to the first and the last node (head and tail):

(1) create a new node (creator) with the given value and an empty value as the address of the next (next)

(2) move the next pointer in the last node pointed to by tail (or the head pointer when tail is empty) to the new node

(3) set the tail pointer to the new node

A next

B next

C head

next V

tail

(37)

Removal at the end Removal at the end

A B

tail C

head

(1) if the list is empty throw an exception and abort

(38)

Removal at the end Removal at the end

A B

tail C

head tmp

(1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(39)

Removal at the end Removal at the end

A B

tail C

head tmp

(1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(40)

Removal at the end Removal at the end

A B

tail C

head tmp

(1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(41)

Removal at the end Removal at the end

A B

tail C

head tmp

killer (1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(42)

Removal at the end Removal at the end

A B

tail C

head tmp

killer (1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(4) set the next next pointer of the node pointed to by tmp (or th head pointer when tmp is empty) to an empty address

(43)

Removal at the end Removal at the end

A B

tail C

head tmp

killer (1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(4) set the next next pointer of the node pointed to by tmp (or th head pointer when tmp is empty) to an empty address

(44)

Removal at the end Removal at the end

A B

tail C

head tmp

killer (1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(4) set the next next pointer of the node pointed to by tmp (or th head pointer when tmp is empty) to an empty address

(5) if there is a pointer to the last node (tail), set it to tmp

(45)

Removal at the end Removal at the end

A B

tail C

head tmp

killer (1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(4) set the next next pointer of the node pointed to by tmp (or th head pointer when tmp is empty) to an empty address

(5) if there is a pointer to the last node (tail), set it to tmp

(46)

Removal at the end Removal at the end

A B

tail C

head tmp

killer (1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(4) set the next next pointer of the node pointed to by tmp (or th head pointer when tmp is empty) to an empty address

(5) if there is a pointer to the last node (tail), set it to tmp (6) release the node pointed by the dedicated pointer (killer)

(47)

Removal at the end Removal at the end

A B

head tmp tail

killer (1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(4) set the next next pointer of the node pointed to by tmp (or th head pointer when tmp is empty) to an empty address

(5) if there is a pointer to the last node (tail), set it to tmp (6) release the node pointed by the dedicated pointer (killer)

(48)

Removal at the end Removal at the end

(1) if the list is empty throw an exception and abort

(2) move the auxiliary pointer (tmp) from the beginning of the list to the penultimate node (the last node whose pointer next is non-empty)

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer if it exists)

(4) set the next next pointer of the node pointed to by tmp (or th head pointer when tmp is empty) to an empty address

(5) if there is a pointer to the last node (tail), set it to tmp (6) release the node pointed by the dedicated pointer (killer)

A B

head tail

Cytaty

Powiązane dokumenty

Although it is possible to read a data byte from a data register without first writing to the Address Pointer Register, if the Address Pointer Register is already at the correct

● input/output streams: iostream, fstream, sstream, iomanip. ● string

● sequence container – double-ended queue, dynamic array using non- contiguous storage, a list of arrays. ● fast addition/removal at the beginning and at the end of

● sort function sorts in the ascending order the contents of a container (between the starting and the ending iterator). ● container items must

The aim is to eliminate (at least partially) the need of using the programming language, introducing instead so-called programming by demonstration, which in

This article describes the case of a patient with full-thickness macular hole formation by a high-power laser pointer, treated with pars plana vitrectomy..

NOKTA / MAKRO PulseDive Scuba + Pointer 2w1 set YEL to najnowsza propozycja tureckiego producenta skierowana dla osób zainteresowanych uniwersalnym urządzeniem typu pinpointer,

TMP-SMX is an oral anti-staphylococcal agent that can be potent in treatment of skin and soft tissue infections (SSTIs) caused by methicillin-resistant Staphylococcus aureus