0% found this document useful (0 votes)
18 views31 pages

CS201 39

This document discusses pointers, references, and dynamic memory allocation in C++. It covers pointers to different data types, references, the new and delete operators for dynamic memory allocation. It also discusses copy constructors, assignment operators, and destructors which must be defined for classes that use dynamic memory allocation to ensure a deep copy of memory is made during object copying and memory is properly freed. The key points are: pointers store memory addresses, new allocates and delete frees dynamic memory, and copy constructors/assignment operators/destructors are needed for classes with dynamic member variables.

Uploaded by

Saeed ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views31 pages

CS201 39

This document discusses pointers, references, and dynamic memory allocation in C++. It covers pointers to different data types, references, the new and delete operators for dynamic memory allocation. It also discusses copy constructors, assignment operators, and destructors which must be defined for classes that use dynamic memory allocation to ensure a deep copy of memory is made during object copying and memory is properly freed. The key points are: pointers store memory addresses, new allocates and delete frees dynamic memory, and copy constructors/assignment operators/destructors are needed for classes with dynamic member variables.

Uploaded by

Saeed ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction to Programming

Lecture 39
Copy
Constructor
Review
 Pointers
 References

 Memory Allocation
Pointers
A pointer is a special type of
variable that contain a memory
address
 Void Pointer
 Pointer to an Integer
 Pointer to a Character
 Pointer to a Float
 Pointer to Objects
References
&
const
Dynamic
Memory
Allocation
Native Operator

 new
 delete
Dynamic Memory Allocation

int *p ;
p = new int ;
delete p ;
Dynamic Memory Allocation

int *p ;
p = new int [ 10 ] ;
delete [ ] p ;
Example
class Matrix
{
private :
int * m ;
int row , col ;
public :
Matrix ( int rows , int cols )
{
m = new int [ rows * cols ] ;
}
};
delete [ ] m ;
Assignment
int i = 0 ;
//Initialization
int i ;
i = 0 ; //Assignment
Matrix m1 , m2 ;
……
m2 = m1 ;
//Assignment Statement
Member to
Member
Assignment
m2 = m1
Pointing to the same
int *m of m1
region in memory
0xefffdad0
mx

int *m of m2
0xefffdad0
Pointing to the same
int *m of m1
region in memory
0xefffdad0
mx

int *m of m2
0xefffdad0
Copy
Constructor
Call by
value
Shallow
Deep Copy
Copy
Matrix ( Matrix & ) ;
class String
Example
{
char * c ;
public :
void copy ( char * s ) ;
String ( char * data ) ;
void print ( ) ;
// etc.
};
Example
String s1 ( “test1” ) ;

String s2 = s1 ;
s1.copy ( “this is a test” ) ;
s2.print ( ) ;
int i ;
i = 10 ;
i=i;
Matrix m2 ( m1 ) ;
Matrix m2 = m1 ;
Rules
For dynamic memory allocation
1. Define copy constructor

2. Write assignment operator

3. Provide destructor
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

You might also like