+ All Categories
Home > Documents > Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How...

Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How...

Date post: 16-Dec-2015
Category:
Upload: augustus-alexander
View: 219 times
Download: 3 times
Share this document with a friend
33
Introduction of Programmin Introduction of Programmin Lecture 28 Lecture 28
Transcript

Introduction of ProgrammingIntroduction of Programming

Lecture 28Lecture 28

Today’s Today’s Lecture Lecture

How memory allocation is done How memory allocation is done inin

C++C++ How is it different from C style How is it different from C style Advantages of memory allocation Advantages of memory allocation

inin C++C++ Uses of memory allocationUses of memory allocation

– ClassesClasses– ObjectsObjects

Pointers to a Pointers to a data structuredata structure

Pointers to Pointers to a class a class

->->

Memory AllocationMemory Allocation

malloc ( ) ;malloc ( ) ;

calloc ( ) ;calloc ( ) ;

realloc ( ) ;realloc ( ) ;

malloc ( 10 * ( sizeof ( int ) ) ) ; malloc ( 10 * ( sizeof ( int ) ) ) ;

free ( )free ( )

newnew

new int ;new int ;

int * iptr ;int * iptr ;

iptr = new int ;iptr = new int ;

Example

new char ;new char ;new double ;new double ;

Example

deletedelete

int * iptr ;int * iptr ;

iptr = new int ;iptr = new int ;

delete iptr ;delete iptr ;

Example

int * iptr ;int * iptr ;

iptr = new int [ 10 ] ;iptr = new int [ 10 ] ;

Example

new data_type [ Number_of_locations ] ;new data_type [ Number_of_locations ] ;

new double [ 10 ] ;new double [ 10 ] ;

Example

int *iptr ;int *iptr ;iptr = new int [ 10 ] ;iptr = new int [ 10 ] ;delete iptr ;delete iptr ;

Example Example

Date *dptr ; Date *dptr ;

dptr is a pointer to an object of type dptr is a pointer to an object of type datedate

Example Example

dptr = new Date ;dptr = new Date ;

main ( )main ( ){{

Date mydate ;Date mydate ;cout<< sizeof cout<< sizeof

( mydate ) ;( mydate ) ;}}

Example Example

int *iptr ;int *iptr ;

iptr = new int [ 10 ] ;iptr = new int [ 10 ] ;

Example Example

Date *dptr ;Date *dptr ;

dptr = new Date [ 10 ] ;dptr = new Date [ 10 ] ;

Example Example

Date date1 , Date date1 , *dptr *dptr ;;date1.setDate ( ) ;date1.setDate ( ) ;

dptr = new Date ;dptr = new Date ;dptr ->setDate ( );dptr ->setDate ( );

dptr.setDate ( ) ;dptr.setDate ( ) ; WrongWrong

Example Example

ProtectedProtected

DestructorDestructor

Allocate enough space for Allocate enough space for

the new datathe new data Populate that spacePopulate that space Delete the previous spaceDelete the previous space Point the new space to thePoint the new space to the

pointer pointing to the pointer pointing to the

original dataoriginal data

char *name =new char [ string_length ]char *name =new char [ string_length ]

delete [ ] namedelete [ ] name

delete [ ] pointer_namedelete [ ] pointer_name

main ( )main ( )

{{

Date mydate ( “01-12-2002” ) ;Date mydate ( “01-12-2002” ) ;

mydate.display ( ) ;mydate.display ( ) ;

}}

Example Example

MessagesMessages

MethodMethod


Recommended