Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How memory allocation is done in C++ C++ How is it different.

Similar presentations


Presentation on theme: "Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How memory allocation is done in C++ C++ How is it different."— Presentation transcript:

1 Introduction of Programming Lecture 28

2 Today’s Lecture How memory allocation is done in How memory allocation is done in C++ C++ How is it different from C style How is it different from C style Advantages of memory allocation in Advantages of memory allocation in C++ C++ Uses of memory allocation Uses of memory allocation – Classes – Objects

3 Pointers to a data structure

4 Pointers to a class

5 ->

6 Memory Allocation

7 malloc ( ) ; calloc ( ) ; realloc ( ) ;

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

9 free ( )

10 new

11 new int ;

12 int * iptr ; iptr = new int ; Example

13 new char ; new double ; Example

14 delete

15 int * iptr ; iptr = new int ; delete iptr ; Example

16 int * iptr ; iptr = new int [ 10 ] ; Example

17 new data_type [ Number_of_locations ] ; new double [ 10 ] ; Example

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

19 Date *dptr ; dptr is a pointer to an object of type date Example

20 dptr = new Date ;

21 main ( ) { Date mydate ; cout<< sizeof ( mydate ) ; } Example

22 int *iptr ; iptr = new int [ 10 ] ; Example

23 Date *dptr ; dptr = new Date [ 10 ] ; Example

24 Date date1, *dptr ; date1.setDate ( ) ; dptr = new Date ; dptr = new Date ; dptr ->setDate ( ); dptr.setDate ( ) ; Wrong Example

25 Protected

26 Destructor

27 Allocate enough space for Allocate enough space for the new data the new data Populate that space Populate that space Delete the previous space Delete the previous space Point the new space to the Point the new space to the pointer pointing to the pointer pointing to the original data original data

28 char *name =new char [ string_length ]

29 delete [ ] name

30 delete [ ] pointer_name

31 main ( ) { Date mydate ( “01-12-2002” ) ; Date mydate ( “01-12-2002” ) ; mydate.display ( ) ; } Example

32 Messages

33 Method


Download ppt "Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How memory allocation is done in C++ C++ How is it different."

Similar presentations


Ads by Google