Date post: | 16-Dec-2015 |
Category: |
Documents |
Upload: | augustus-alexander |
View: | 219 times |
Download: | 3 times |
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
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
main ( )main ( ){{
Date mydate ;Date mydate ;cout<< sizeof cout<< sizeof
( mydate ) ;( mydate ) ;}}
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
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
main ( )main ( )
{{
Date mydate ( “01-12-2002” ) ;Date mydate ( “01-12-2002” ) ;
mydate.display ( ) ;mydate.display ( ) ;
}}
Example Example