Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Programming Lecture 39: Pointers, References, and Memory Allocation, Slides of Computer Programming

In this lecture, learn about pointers, references, and dynamic memory allocation in programming. Topics include void pointers, pointers to integers, characters, floats, and objects, dynamic memory allocation using 'new' and 'delete', and the importance of defining copy constructors, assignment operators, and destructors for classes that use dynamic memory.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

somo
somo 🇮🇳

4.8

(4)

70 documents

1 / 31

Toggle sidebar

Related documents


Partial preview of the text

Download Programming Lecture 39: Pointers, References, and Memory Allocation and more Slides Computer Programming in PDF only on Docsity! Introduction to Programming Lecture 39 Docsity.com Copy Constructor Docsity.com  Void Pointer  Pointer to an Integer  Pointer to a Character  Pointer to a Float  Pointer to Objects Docsity.com References Docsity.com Docsity.com Native Operator  new  delete Docsity.com int *p ; p = new int ; delete p ; Dynamic Memory Allocation Docsity.com int *p ; p = new int [ 10 ] ; delete [ ] p ; Dynamic Memory Allocation Docsity.com Assignment Docsity.com int i = 0 ; //Initialization int i ; i = 0 ; //Assignment Docsity.com Matrix m1 , m2 ; …… m2 = m1 ; //Assignment Statement Docsity.com 0xefffdad0 mx Pointing to the same region in memory int *m of m1 0xefffdad0 int *m of m2 Docsity.com 0xefffdad0 mx Pointing to the same region in memory int *m of m1 0xefffdad0 int *m of m2 Docsity.com Copy Constructor Docsity.com Matrix ( Matrix & ) ; Docsity.com Example class String { char * c ; public : void copy ( char * s ) ; String ( char * data ) ; void print ( ) ; // etc. } ; Docsity.com Example String s1 ( “test1” ) ; String s2 = s1 ; s1.copy ( “this is a test” ) ; s2.print ( ) ; Docsity.com Rules For dynamic memory allocation 1. Define copy constructor 2. Write assignment operator 3. Provide destructor Docsity.com What have we covered today  Review of pointers – Dynamic memory allocation  new  Delete  For memory allocation in classes we must provide – Constructor – Copy constructor – Assignment operator – destructor Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved