ΗΥ-150. Προγραμματισμός

Σχετικά έγγραφα
ΗΥ-150. Προγραμματισμός

ΗΥ-150. Προγραµµατισµός. υναµική ιαχείριση Μνήµης (1/2)

ΗΥ-150. Προγραμματισμός

Διάλεξη 13η: Δυναμική Διαχείρηση Μνήμης, μέρος 1

Προγραμματισμός Ι. Δυναμική Διαχείριση Μνήμης. Δημήτρης Μιχαήλ. Ακ. Έτος Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

HY150a Φροντιστήριο 3 24/11/2017

Διάλεξη 9: Δυναμική Δέσμευση Μνήμης

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

ΕΡΓΑΣΤΗΡΙΟ 1 - ΣΗΜΕΙΩΣΕΙΣ

Η γλώσσα προγραμματισμού C Δυναμική διαχείριση μνήμης

Δυναμική δέσμευση και αποδέσμευση μνήμης. Προγραμματισμός II 1

Ανάπτυξη και Σχεδίαση Λογισμικού

Lab 1: C/C++ Pointers and time.h. Panayiotis Charalambous 1

Lab 1: C/C++ Pointers and time.h. Panayiotis Charalambous 1

ΗΥ-150. Προγραμματισμός

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

Προγραμματισμός Υπολογιστών με C++

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

ΠΛΗ111. Ανοιξη Μάθηµα 1 ο Ανασκόπηση της Γλώσσας Προγραµµατισµού C. Τµήµα Ηλεκτρονικών Μηχανικών και Μηχανικών Υπολογιστών Πολυτεχνείο Κρήτης

ΕΠΛ232 Προγραμματιστικές Τεχνικές και Εργαλεία Δυναμική Δέσμευση Μνήμης (Κεφάλαιο , KNK-2ED)

The Simply Typed Lambda Calculus

Προγραμματισμός Υπολογιστών με C++

Αναφορές, είκτες και Αλφαριθμητικά

Section 8.3 Trigonometric Equations

Προγραμματισμός Ι. Εγγραφές. Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

Μνήμη Διευθύνσεις Δείκτες. Προγραμματισμός II 1

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Φροντιςτήριο. Linked-List

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

Προγραμματισμός Υπολογιστών με C++

2 Composition. Invertible Mappings

(C) 2010 Pearson Education, Inc. All rights reserved.

Προγραμματισμός Η/Υ (ΤΛ2007 )

Προγραμματισμός Ι. Πίνακες, Δείκτες, Αναφορές και Δυναμική Μνήμη. Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

Διάλεξη 5: Δείκτες και Συναρτήσεις

Εισαγωγή στον Προγραμματισμό

Διαδικασιακός Προγραμματισμός

Διδάσκων: Κωνσταντίνος Κώστα Διαφάνειες: Δημήτρης Ζεϊναλιπούρ

ΕΠΛ232 Προγραμματιστικές Τεχνικές και Εργαλεία Δείκτες και Συναρτήσεις (Κεφάλαιο 11, KNK-2ED)

ΕΙΣΑΓΩΓΗ ΣΤΟN ΠΡΟΓΡΑΜΜΑΤΙΣΜΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΠΟΛΥΤΕΧΝΙΚΗ ΣΧΟΛΗ ΤΜΗΜΑ ΜΗΧΑΝΙΚΩΝ Η/Υ ΚΑΙ ΠΛΗΡΟΦΟΡΙΚΗΣ

Block Ciphers Modes. Ramki Thurimella

Homework 3 Solutions

Α' Εξάμηνο ΕΙΣΑΓΩΓΗ ΣΤΟ ΔΟΜΗΜΕΝΟ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟ

Κλήση Συναρτήσεων ΚΛΗΣΗ ΣΥΝΑΡΤΗΣΕΩΝ. Γεώργιος Παπαϊωάννου ( )

Finite Field Problems: Solutions

Main source: "Discrete-time systems and computer control" by Α. ΣΚΟΔΡΑΣ ΨΗΦΙΑΚΟΣ ΕΛΕΓΧΟΣ ΔΙΑΛΕΞΗ 4 ΔΙΑΦΑΝΕΙΑ 1

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 5: Component Adaptation Environment (COPE)

Διάλεξη 9η: Πίνακες (arrays)

Προγραμματισμός Ι (ΗΥ120)

Διάλεξη 10: Δομές Δεδομένων Ι (Στοίβες & Ουρές)

Η γλώσσα C. Δείκτες και Διαχείριση Μνήμης (memory management)

Συµβολοσειρές - Strings

Assalamu `alaikum wr. wb.

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

EE512: Error Control Coding

Διδάσκων: Παναγιώτης Ανδρέου

ΔΕΙΚΤΕΣ ΚΑΙ ΔΙΕΥΘΥΝΣΕΙΣ

Instruction Execution Times

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Κλάσεις και Αντικείμενα Αναφορές

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

Εισαγωγή στους Η/Υ. Γιώργος Δημητρίου. Μάθημα 3-4: Προγραμματισμός MIPS. Πανεπιστήμιο Θεσσαλίας - Τμήμα Πληροφορικής

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

Example Sheet 3 Solutions

derivation of the Laplacian from rectangular to spherical coordinates

Partial Trace and Partial Transpose

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

Προγραμματισμός Δομές Δεδομένων

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Μορφοποίηση υπό όρους : Μορφή > Μορφοποίηση υπό όρους/γραμμές δεδομένων/μορφοποίηση μόο των κελιών που περιέχουν/

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Αναφορές Στοίβα και Σωρός Μνήμης Αντικείμενα ως ορίσματα

Προγραμματισμός Η/Υ (ΤΛ2007 )

Διάλεξη 14: Δομές Δεδομένων ΙΙI (Λίστες και Παραδείγματα)

Προγραμματισμός Ι. Κλάσεις και Αντικείμενα. Δημήτρης Μιχαήλ. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

Διάλεξη 11η: Δείκτες, μέρος 1

Γλώσσα Προγραμματισμού C++ Εισαγωγή - Μια πρώτη ματιά

Physical DB Design. B-Trees Index files can become quite large for large main files Indices on index files are possible.

Partial Differential Equations in Biology The boundary element method. March 26, 2013

ΗΥ-150. Πίνακες (Arrays)

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Ανάπτυξη Μεγάλων Εφαρµογών στη Γλώσσα C (2)

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 11/3/2006

υναµική διαχείριση µνήµης στη C++ Στην ενότητα αυτή θα µελετηθούν τα εξής επιµέρους θέµατα: ΕΠΛ 132 Αρχές Προγραµµατισµού ΙΙ 2 είκτες

Διάλεξη 12η: Δείκτες, μέρος 2

Απόκριση σε Μοναδιαία Ωστική Δύναμη (Unit Impulse) Απόκριση σε Δυνάμεις Αυθαίρετα Μεταβαλλόμενες με το Χρόνο. Απόστολος Σ.

Συναρτήσεις και Πίνακες

Advanced Subsidiary Unit 1: Understanding and Written Response

Fractional Colorings and Zykov Products of graphs

C.S. 430 Assignment 6, Sample Solutions

Math 6 SL Probability Distributions Practice Test Mark Scheme

Συστήματα Διαχείρισης Βάσεων Δεδομένων

Κεφάλαιο , 3.2: Συναρτήσεις II. (Διάλεξη 12)

Διδάσκων: Παναγιώτης Ανδρέου

Exercises 10. Find a fundamental matrix of the given system of equations. Also find the fundamental matrix Φ(t) satisfying Φ(0) = I. 1.

Section 7.6 Double and Half Angle Formulas

Εισαγωγή στον Προγραμματισμό

Οι δείκτες στη γλώσσα C

Εισαγωγή στον Προγραμματισμό

PARTIAL NOTES for 6.1 Trigonometric Identities

Transcript:

ΗΥ-150 Δυναμική Διαχείριση Μνήμης

Δυναμική Διαχείριση Μνήμης Μέχρι τώρα στατική ανάθεση και δέσμευση μνήμης Ζητούσαμε στο πρόγραμμά μας τη μέγιστη μνήμη που μπορεί να χρειαζόμασταν #define MAX_SIZE 10000 int array[max_size]; Μειονέκτημα : Υπάρχουν εφαρμογές που το MAX_SIZE είναι άγνωστο οπότε αναγκαζόμαστε να δηλώνουμε μεγάλες τιμές και να χάνουμε και σε μνήμη και σε ταχύτητα Δυναμική κατανομή μνήμης (dynamic memory allocation) Αίτηση από το λειτουργικό να μας παραχωρήσει μνήμη Ειδοποίηση στο λειτουργικό ότι δεν χρειαζόμαστε πια ένα μέρος μνήμης Ό,τι δεσμεύετε πρέπει να αποδεσμεύετε

Γιατί χρειαζόμαστε δυναμική μνήμη; Διότι ο αριθμός των δεδομένων μπορεί να μην είναι γνωστός τη στιγμή της μετάφρασης. Π.χ. Να εξαρτάται από την είσοδο (π.χ. ο χρήστης επιλέγει να δώσει Ν αριθμούς) Μπορεί να εξαρτάται από κάποιο ενδιάμεσο υπολογιστικό αποτέλεσμα Τα δεδομένα μπορεί να αυξομειώνονται κατά την εκτέλεση του προγράμματος

Γιατί χρειαζόμαστε δυναμική μνήμη; Βάση για πιο πολύπλοκες δομές δεδομένων Intro to Pointers and Memory in C Dynamic

Κατηγορίες Μνήμης του Προγράμματος Μνήμη αποθήκευσης του κώδικα του προγράμματος Μνήμη αποθήκευσης των μεταβλητών ενός προγράμματος που δεσμεύονται με στατική ή αυτόματη διαχείριση μνήμης Αυτή η μνήμη ονομάζεται στοίβα (stack) Μνήμη αποθήκευσης των μεταβλητών ενός προγράμματος που δεσμεύονται με δυναμική διαχείριση μνήμης Αυτή η μνήμη ονομάζεται σωρός (heap)

Memory layout of programs 0 Header info 100 all malloc()s 400 560 Code Data - Heap Dynamic memory 1010 all normal vars 1200 Data - stack Local memory + function call stack

Συναρτήσεις Δυναμικής Διαχείρισης Μνήμης void *malloc( size_t numbytes); Δεσμεύει numbytes διαδοχικά bytes και επιστρέφει δείκτη στην αρχή της δεσμευμένης μνήμης Επιστρέφει NULL αν δε μπορεί να δεσμεύσει τη μνήμη int *ptr; ptr = (int *) malloc (100*sizeof(int)); 7

Συναρτήσεις Δυναμικής Διαχείρισης Μνήμης void free(void *pointer); Αποδεσμεύει τη μνήμη που έχει δεσμευτεί με μία από τις προηγούμενες κλήσεις στην malloc, calloc ή realloc Δεν πρέπει να αποδεσμεύουμε πάνω από μια φορά την ίδια μνήμη ptr = (int *)malloc (100*sizeof(int)); free(ptr); // Αυτό είναι οκ μέχρι εδώ free(ptr); // Αποδεσμεύουμε την ίδια μνήμη // δυο φορές! ΛΑΘΟΣ 8

Δυναμική Διαχείριση Μνήμης Παράδειγμα #include <stdlib.h> int main(void) { int *ptr; /* allocate space to hold an int */ ptr = (int *) malloc(sizeof(int)); /* do stuff with the space */ *ptr=4; } free(ptr); /* free up the allocated space */

Δυναμικός πίνακας int *ptr, i,size; cout << Enter the size of the array\n ; cin >> size; ptr = (int *) malloc( size * sizeof(int) ); for(i=0; i<size; i++){ ptr[i] = i; } 10

Συναρτήσεις Δυναμικής Διαχείρισης Μνήμης void *calloc(size_t nelements, size_t bytes); Δεσμεύει nelements διαδοχικά αντικέιμενα το καθένα μεγέθους bytes και επιστρέφει δείκτη στην αρχή της δεσμευμένης μνήμης Επιστρέφει NULL αν δε μπορεί να δεσμεύσει τη μνήμη Αρχικοποιεί τα περιεχόμενα της δεσμευμένης μνήμης στο 0 Παρόμοια με την malloc Διαφορά 1: λίγο διαφορετική κλήση Διαφορά 2: αρχικοποίηση των περιεχομένων με 0 int *ptr = (int *) calloc (100, sizeof(int)); 11

Συναρτήσεις Δυναμικής Διαχείρισης Μνήμης void *realloc(void *ptr, size_t size); Επέκταση μνήμης Ο δείκτης *ptr πρέπει να δείχνει σε μνήμη ήδη δεσμευμένη με την malloc ή calloc ptr = (int *)malloc (100*sizeof(int)); Επαναδέσμευση μνήμης με άλλο μέγεθος ptr = (int *)realloc(ptr, 200*sizeof(int)); Τα προηγούμενα περιεχόμενα της δεσμευμένης μνήμης διατηρούνται (τουλάχιστον όσα χωρούν στην καινούργια) Η καινούργια μνήμη μπορεί να είναι σε άλλη διεύθυνση αν δεν υπήρχε αρκετά μεγάλο συνεχόμενο block από ελεύθερη μνήμη για δέσμευση. Σε αυτήν την περίπτωση γίνεται μεταφορά των δεδομένων και αποδεσμεύεται η μνήμη ptr, σαν να γίνεται δηλαδή μια free(ptr) 12

Ο τύπος void* Γενικός δείκτης Κατά την εκτέλεση του προγράμματος διευκρινίζεται ο τύπος του, π.χ. εάν θα είναι char *, int *, float *, double *, struct my_struct *, κ.ο.κ. Ο γενικός δείκτης μας δίνει τη δυνατότητα να ορίζουμε συναρτήσεις που μας επιστρέφουμε χώρο στη μνήμη χωρίς να μας ενδιαφέρει ο τύπος! Π.χ. malloc() ΠΡΟΣΟΧΗ: σε συναρτήσεις είναι τελείως διαφορετικό 1. void f() // η συνάρτηση ΔΕΝ επιστρέφει τίποτα 2. void *f() //επιστρέφει ένα δείκτη που ο προγραμματιστής πρέπει να //αποσαφηνίσει τον τύπο του μέσα στην συνάρτηση 13

Δυναμικοί Πίνακες Πίνακες που το μέγεθος τους καθορίζεται κατά την εκτέλεση του προγράμματος (σε run-time). Δυνατότητα προσαρμογής στις εκάστοτε απαιτήσεις χωρίς ανάγκη περιορισμού. int *x = (int *)malloc( 10 * sizeof(int)); x 14

Πρόβλημα 1: Αποτυγχάνει η Δέσμευση Πρέπει να γίνεται πάντα έλεγχος επιστροφής της απαραίτητης μνήμης!! Είναι ευθύνη του προγραμματιστή και στοιχείο σωστού προγραμματισμού! Οι συναρτήσεις δυναμικής δέσμευσης επιστρέφουν NULL όταν αποτυγχάνουν, το οποίο μπορούμε να εξετάσουμε και να διακόψουμε την εκτέλεση του προγράμματος αν χρειάζεται int *p = (int *)malloc(10000*sizeof(int)); if (p == (int *) NULL) { printf( No Memory Available\n ); exit(0); } 15

Πρόβλημα 2: Διαρροή Μνήμης Απελευθέρωση Πρέπει να ελευθερώνουμε την μνήμη όταν δεν την χρειαζόμαστε (με την free) Δεν ελευθερώνουμε ποτέ μνήμη που δεν έχει αποκτηθεί δυναμικά (λ.χ. με malloc) Αν δεν ελευθερώσουμε όλη τη μνήμη που δεσμεύουμε αυτό ονομάζεται διαρροή μνήμης (memory leak) Η διαρροή μνήμης μπορεί να οδηγήσει σε καταστάσεις που δεν υπάρχει άλλη ελεύθερη μνήμη Προχωρημένο: όταν τελειώνει η ελεύθερη μνήμη του υπολογιστή, τότε το λειτουργικό στέλνει κάποια μνήμη στον δίσκο, ελευθερώνει έτσι μνήμη και την ξανα-χρησιμοποιεί. Ξαναφέρνει από τον δίσκο στη μνήμη όταν γίνεται πρόσβαση σε αυτήν. Αυτό οδηγεί στο φαινόμενο trashing όπου γράφεται και διαβάζεται από το δίσκο συνέχεια μνήμη. Με διαρροή μνήμης ένα πρόγραμμα μπορεί να αρχίσει να κάνει thrashing μετά από ώρες, μέρες, ή χρόνια συνεχής λειτουργίας του. Λύση : Αποδεσμεύουμε αμέσως τη μνήμη που δεν χρησιμοποιούμε (χρήση της free) 16

Πρόβλημα 3: Χρήση Μετά την Αποδέσμευση Κάνουμε free και μετά ξαναχρησιμοποιούμε την ίδια μνήμη Παρόμοια αποτελέσματα όταν χρησιμοποιούμε μη δεσμευμένη μνήμη Αλλάζουμε μεταβλητές που χρησιμοποιούμε χωρίς να το ξέρουμε δύσκολα ανιχνεύεται Πάμε να γράψουμε σε μνήμη που δεν μας ανήκει (segmentation fault) και το πρόγραμμα διακόπτεται αυτόματα 17

Πρόβλημα 4: Αποδέσμευση μνήμης που δεν έχουμε δεσμεύσει Μπορεί να μπερδέψει το λειτουργικό στην διαχείριση ελεύθερης μνήμης 18

Πρόβλημα 5: Dangling pointers int *i, *x; i = (int*)malloc( 5 x sizeof(int)); x = i; //both point to the same address. free(x); x = NULL; i = NULL; // One way to prevent incorrect access 19

Καλές πρακτικές Αρχικοποίηση Προσμέτρηση του '\0' στο μέγεθος strings. Έλεγχος τιμής επιστρογής της malloc() Για κάθε malloc() υπάρχει και η αντίστοιχη free() Το &variable είναι ένας pointer Μετά την αποδεύσμευση ενός pointer εκχώρηση της τιμής NULL σε αυτόν Π.χ. int * pint=(int *)malloc(sizeof(int)); /* Do something with pint */ free(pint) ; pint = NULL; Για «ασφάλεια» και αποφυγή κατά λάθος επαναχρησιμοποίησης της.

Pointers ως παράμετροι #include <iostream> #include <cstdlib> using namespace std; int sumandinc(int *pa, int *pb,int* pc); int main(){ int a=4, b=5, c=6; int *ptr = &b; /* call the function */ int total = sumandinc(&a,ptr,&c); cout << The sum of << a << and << b << is << total << and c is << c << endl; } /* pointers as arguments */ int sumandinc(int *pa, int *pb,int *pc ){ *pc = *pc+1; // return a pointee value; NOT *(pc+1) return (*pa+*pb); /* return by value */ } 21

int main(int argc, char *argv[]) { int *p; DoSomething( &p ); cout <<*p <<endl; /* will this work? */ return 0; } /* passed and returned using pointers */ void DoSomething(int **ptr){ int temp= 8; *ptr = (int*)malloc(sizeof(int)); **ptr = temp; }

Σύγκριση pointers Είναι δυνατή ανεξαρτήτων που δείχνουν. Έχει νόημα όταν δείχνουν σε στοιχεία πίνακα. Γίνεται με τους τελεστές <, >, =, <= and >= Σύγκριση άσχετων μεταξύ τους pointers είναι απρόβλεπτη. int data[100], *p1, *p2; for (int i = 0; i <100;i++) p1 = &data [1]; p2 = &data [2]; if (p1 > p2) else data[i] = i; cout<<"\n\n p1 is greater than p2"; cout<<"\n\n p2 is greater than p1"; 23

Operators new and new[] pointer = new type pointer = new type [number_of_elements] The first expression is used to allocate memory to contain one single element of type type. The second one is used to assign a block (an array) of elements of type type, where number_of_elements is an integer value representing the amount of these. For example: int * bobby; bobby = new int [5]; In this case, the system dynamically assigns space for five elements of type int and returns a pointer to the first element of the sequence, which is assigned to bobby. Therefore, now, bobby points to a valid block of memory with space for five elements of type int.

Ανεπάρκεια μνήμης int * bobby; bobby = new (nothrow) int [5]; if (bobby == 0) { }; // error assigning memory. Take measures.

Operators delete and delete[] Since the necessity of dynamic memory is usually limited to specific moments within a program, once it is no longer needed it should be freed so that the memory becomes available again for other requests of dynamic memory. This is the purpose of the operator delete, whose format is: delete pointer; delete [] pointer; The first expression should be used to delete memory allocated for a single element, and the second one for memory allocated for arrays of elements. The value passed as argument to delete must be either a pointer to a memory block previously allocated with new, or a null pointer (in the case of a null pointer, delete produces no effect).

#include <iostream> #include <new> using namespace std; int main () { int i,n; int *p; cout << "How many numbers would you like? "; cin >> i; p= new (nothrow) int[i]; if (p == NULL) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } } } return 0; cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; How many numbers would you like? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32,

Δείκτες σε δείκτες char ch; char *pch; char **ppch; char ***ppch; Δείκτης σε πίνακα από δείκτες (άγνωστο στην αρχή και το μέγεθος του πίνακα και το μέγεθος των περιοχών που δείχνουν οι δείκτες) 28

Παράδειγμα 2Δ πίνακα - C++ int **dynamicarray = 0; //memory allocated for elements of rows. dynamicarray = new int *[ROWS] ; //memory allocated for elements of each column. for( int i = 0 ; i < ROWS ; i++ ) dynamicarray[i] = new int[columns]; //free the allocated memory for( int i = 0 ; i < ROWS ; i++ ) delete [] dynamicarray[i] ; delete [] dynamicarray ; 29

Δείκτες σε δείκτες C++ #include <iostream> #include <cstdlib> using namespace std; int main(void) { int lines = 5; /* Both lines and cols could be evaluated */ int cols = 10; /* or read in at run time */ int i, **Α; A = new int *[lines] ; if (A == NULL) { cout<<"failure to allocate room for row pointers.\n"; exit(0); } for (i = 0; i < lines; i++) { Α[i] = = new int[cols]; if (Α[i] == NULL) { cout<<"\nfailure to allocate for row <<i<<endl; exit(0); } } //free the memory for (i = 0; i < lines; i++) delete [] Α[i]; delete [] A; return 0; } Εφαρμογή : πίνακας - εικόνα A A[0] A[1] A[2] A[3] A[4] A[0][0] A[0][1] A[0][2] A[1][0]... A[2][0]... A[3][0]... A[4][0]... A[i][j] A[4][9] 30

Δείκτες σε δείκτες - C #include <iostream> #include <cstdlib> using namespace std; int main(void) { int lines = 5; /* Both lines and cols could be evaluated */ int cols = 10; /* or read in at run time */ int i, **Α; A = (int **)malloc(lines * sizeof(int *)); if (A == NULL) { cout<<"failure to allocate room for row pointers.\n"; exit(0); } for (i = 0; i < lines; i++) { Α[i] = (int *) malloc(cols * sizeof(int)); if (Α[i] == NULL) { cout<<"\nfailure to allocate for row <<i<<endl; exit(0); } } //free the memory for (i = 0; i < lines; i++) free (Α[i]); free(a); return 0; } Εφαρμογή : πίνακας - εικόνα A A[0] A[1] A[2] A[3] A[4] A[0][0] A[0][1] A[0][2] A[1][0]... A[2][0]... A[3][0]... A[4][0]... A[i][j] A[4][9] 31

Συνοπτικά Έχουμε: int x; int *px; px = &x Και ένα ακόμα βήμα: int **ppx; ppx = &px; Οπότε μπορούμε να κάνουμε: **ppx = 42; /* Puts 42 into x */ Αλλά όχι: *ppx = 42; /* ERROR!! */ 32

Παραδείγματα ADDR CONT SYMB 1000 42 x 2000 1000 px int x; int *px; int **ppx; px = &x ppx = &px; **ppx = 42; 3000 2000 ppx 33

ΠΡΟΣΟΧΗ! Δείκτες σε Δείκτες έναντι Δυσδιάστατους Πίνακες int a[10][20]; a[r][c] είναι συντακτικά αποδεκτό Δέσμευση μνήμης: 10 20 ακέραιοι Υπολογισμός διεύθυνσης του a[r][c]: r*20+c+αρχή του πίνακα int **b; b[r][c] είναι συντακτικά αποδεκτό Δέσμευση μνήμης για b:10 δείκτες b = (int **)malloc (10*sizeof(int*)) Δέσμευση μνήμης για b[i]: 20 ακέραιοι σε κάθε γραμμή for (i = 0; i < 10; i++) b[i] = (int *) malloc(20 * sizeof(int)); Υπολογισμός διεύθυνσης του b[r][c]: διεύθυνση μνήμης του δείκτη b[r] + c ΠΡΙΝ ΤΟΝ ΥΠΟΛΟΓΙΣΜΟ ΤΟΥ b[r][c] πρέπει να έχει γίνει σωστή ανάθεση τιμών στα b[r] και δέσμευση μνήμης. 34

Strings Χρήση δεικτών για την διαχείριση strings Όπως έχουμε πει: int a[ ] = {0, 1, 3, 5} Και η διάσταση θα καθοριστεί σε a[4] Επίσης: char b[ ] = { t, e, s, t, \0 } char c[ ] = test ; 35

Όμως char aline[ ] = Hi, there! ; char *pline = Hi, there! ; Παρόμοια αλλά όχι τα ίδια!!! Το πρώτο καθορίζει έναν πίνακα χαρακτήρων (11) που μπορεί να αλλαχθεί: aline[4] = T ; /* Σωστό! */ Το δεύτερο καθορίζει έναν δείκτη προς μία σταθερά τύπου string που δεν μπορεί να αλλαχθεί: pline[4] = T ; /* ΛΑΘΟΣ!! */ Ενώ αν: pline = aline; pline[4] = T ; /* Σωστό! */ 36

Κι άλλα Όχι μόνο δείκτες σε δείκτες αλλά και πίνακες από δείκτες! char *name[ ] = { Illegal, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; Π.χ. name[2] είναι ένας δείκτης στο character string F, e, b, \0 Γιατί θα θέλαμε έναν πίνακα από δείκτες; 37

Φανταστείτε : Έχουμε ονόματα όπως: Smith, David Leahy, Bill Dagon, David Humperdinck, Englebert Υποθέστε πως θέλουμε να τα ταξινομήσουμε αλφαβητικά αλλά παρατηρούμε πως είναι διαφορετικού μεγέθους. Αντί να χρησιμοποιήσουμε έναν αλγόριθμο ταξινόμησης που αλλάζει character strings around γιατί να μην χρησιμοποιήσουμε έναν πίνακα από δείκτες και να αλλάζουμε δείκτες; πολύ πιο γρήγορο 38

Έχουμε έναν πίνακα από pointers name[0] name[1] name[2] name[3] Smith, David Leahy, Bill Dagon, David Humperdinck, Englebert 39

Ταξινόμηση με μετακίνηση δεικτών name[0] name[1] name[2] name[3] Smith, David Leahy, Bill Dagon, David Humperdinck, Englebert 40

struct emploee *A; int N; cin >> N; A = (struct emploee *) malloc(n*sizeof(struct emploee)); A[100] free(a);

Dynamic Memory Allocation Static Allocation, in C int g_x; int main(void) { int x = 1; int a[20];... } g_x may be put on the stack or allocated statically at compile time x is pushed on the stack when main() is invoked a (all 20 elements) is also pushed on the stack when main() is invoked The size of a is also fixed at compile time - Stack space may be limited - May not know how much data must be stored at compile time

Dynamic Memory Allocation Dynamic Allocation - Reserves memory from a much larger free store, or heap - Gives the programmer a pointer to refer to this memory x = malloc ( sizeof (HDS)); function(x); x Huge Data Structure Only the pointer x is pushed onto the stack Call-by-reference

The Heap: Allocation heap memory manager I need a block of memory of size n Here is a pointer to the block user code

The Heap: Deallocation heap I no longer need this memory memory manager user code

The Heap: Deallocation heap I no longer need this memory memory manager user code

Manual Allocation Memory deallocation driven through explicit requests to allocate and deallocate memory is called manual allocation void fn(void) { char* c; c[0] = a ; } void fn(void) { char* c = new char[10]; c[0] = a ; } Some languages include facilities for the automatic deallocation of memory; these facilities are called garbage collectors

Allocation Method: First Fit - The first block in the heap that is big enough is allocated first - Each free block keeps a pointer to the next free block - These pointers may be adjusted as memory is allocated We can keep track of free space in a separate list called the free list

Allocation Method: First Fit - Sorting the free list may result in performance benefits - Increasing size: Scanning the free list to allocate new memory will always pick the smallest free block x = malloc(10); 2 4 4 6 8 12 20 64 528

Allocation Method: First Fit - Sorting the free list may result in performance benefits - Decreasing size: Scanning the free list to allocate new memory will always pick the first block since it is the largest x = malloc(128); x = malloc(512); No contiguous block exists 4 4 46 68 12 8 12 20 20 64 400 64 528

Coalescence - When two free blocks are adjacent to each other, they can be merged into a single block - This process is called coalescence

Allocation Method: Buddy System 8k 16k 8k 12k 16k 64k block 32k - When a block is allocated, memory is divided into blocks, giving each block a same-size buddy - If a block cannot be subdivided, memory can be wasted - Coalescence of memory can be done quickly at the expense of this wasted memory

Allocation Method: Suballocators reserved by suballocator heap - Suballocators are user code which reserve the system-provided heap and then manage that memory itself - System memory management must operate in a general fashion - Suballocators can take advantage of known characteristics of the application s memory needs to speed the process - e.g., equal-sized blocks

Memory Management Issues - Dangling Pointers (widows) char* c = new char[10];... Memory is allocated, c points to that block delete[] c;... c[0] = a ; c = 0; c s memory is deallocated c is dereferenced, accessing memory that was already deallocated This problem can occur in any language which uses explicit deallocation

Memory Management Issues Memory Leaks (orphans) void fn(void) { char* c = new char[10];... delete[] c; return; } Memory is allocated, and c points to that block c falls out of scope without the memory it points to being deallocated If no other pointer points to this memory, it can no longer be referenced, and thus cannot be deallocated. With repeated application, eventually the entire heap will fill up with unreachable allocated memory!

Memory Management Issues Heap Fragmentation Internal heap fragmentation results from the allocation of wasted space by the memory manager Some heap schemes waste space by placing allocation data in the heap (size of allocated blocks, etc.) Where heap data is required to be aligned to particular byte boundaries, additional padding may be allocated to match those boundaries

Memory Management Issues Heap Fragmentation External heap fragmentation results from repeated allocation and deallocation from the heap Can be corrected by periodic compaction of the heap and coalescence of the resulting adjacent free blocks

Memory Management Issues Heap Fragmentation (external) heap A newly allocated block can t fit in the available free blocks??? But by compacting the heap......a larger contiguous block is created!

Memory Management Issues External Heap Fragmentation But what about all the pointers referring to the moved blocks of memory? If the system knows the locations of every pointer to every allocated block, then it can adjust those pointers accordingly. Some systems are not privy to such information; one solution is to use handles to allocated blocks rather than returning pointers directly to those blocks.

Memory Management issues heap If the heap is compacted but pointers to blocks in the heap are not changed......the pointers will point to other blocks or to no blocks at all once the blocks are compacted.

Memory Management issues heap handles By using handles to blocks, the handles pointers, which are known, can be changed. handles The handles given to the program stay the same.