CS201-28 - Introduction of Programming Lecture 28 Today’s...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction of Programming Lecture 28 Today’s Lecture How memory allocation is done in C++ How is it different from C style Advantages of memory allocation in C++ Uses of memory allocation – Classes – Objects Pointers to a data structure Pointers to a class ­> Memory Allocation malloc ( ) ; calloc ( ) ; realloc ( ) ; malloc ( 10 * ( sizeof ( int ) ) ) ; free ( ) new new int ; Example int * iptr ; iptr = new int ; Example new char ; new double ; delete Example int * iptr ; iptr = new int ; delete iptr ; Example int * iptr ; iptr = new int [ 10 ] ; Example new data_type [ Number_of_locations ] ; new double [ 10 ] ; Example int *iptr ; iptr = new int [ 10 ] ; delete iptr ; Example Date *dptr ; dptr is a pointer to an object of type date dptr = new Date ; Example main ( ) { } Date mydate ; cout<< sizeof ( mydate ) ; Example int *iptr ; iptr = new int [ 10 ] ; Example Date *dptr ; dptr = new Date [ 10 ] ; Date date1 , *dptr ; date1.setDate ( ) ; Example dptr = new Date ; dptr ­>setDate ( ); dptr.setDate ( ) ; Wrong Protected Destructor Allocate enough space for the new data Populate that space Delete the previous space Point the new space to the pointer pointing to the original data char *name =new char [ string_length ] delete [ ] name delete [ ] pointer_name main ( ) { Date mydate ( “01­12­2002” ) ; mydate.display ( ) ; } Example Messages Method ...
View Full Document

This note was uploaded on 01/12/2010 for the course CS CS 201 taught by Professor Dr.naveedmalik during the Spring '09 term at Virtual University of Pakistan.

Ask a homework question - tutors are online