Docsity

Memory Allocation in C++: Pointers and Dynamic Allocation, Slides of Computer Programming

Partial preview of the text

Download Memory Allocation in C++: Pointers and Dynamic Allocation and more Slides Computer Programming in PDF only on Docsity!

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 class

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

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

new

new int ;

new char ; new double ; Example

delete

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

new data_type [ Number_of_locations ] ;

new double [ 10 ] ;

Example

Date *dptr ;

dptr is a pointer to an object of type date

Example

dptr = new Date ;