Cmsc 202
This presentation is the property of its rightful owner.
Sponsored Links
1 / 19

CMSC 202 PowerPoint PPT Presentation


  • 71 Views
  • Uploaded on
  • Presentation posted in: General

CMSC 202. Computer Science II for Majors. Topics. Memory management operators Dynamic memory Project 2 questions. Memory Management Operators. Control allocation and deallocation of memory for any data-type Used when we don’t know in advance how much memory space is needed

Download Presentation

CMSC 202

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cmsc 202

CMSC 202

Computer Science II for Majors


Topics

Topics

  • Memory management operators

  • Dynamic memory

  • Project 2 questions


Memory management operators

Memory Management Operators

  • Control allocation and deallocation of memory for any data-type

  • Used when we don’t know in advance how much memory space is needed

  • C++ defines two unary operators – new and delete for allocating and deallocating memory


Memory management operators cont

Memory Management Operators …cont

  • new operator

  • General Form:

    pointer-variable = newdata-type

  • pointer-variable is pointer of type data-type

  • data-type is any valid data type (including objects)


Memory management operators cont1

Memory Management Operators …cont

  • Example

    1. float *floatPtr;

    floatPtr = new float;

    2. int *arrayPtr;

    arrayPtr = new int[10];

    3. Time *timePtr;

    timePtr = new Time;


Memory management operators cont2

Memory Management Operators …cont

  • Consider pointer to array example

  • It creates a memory space for an array of 10 integers

  • arrayPtr[0] refers to first element, arrayPtr[1] refers second element…

  • Multidimensional arrays

    int **arrayPtr = new int[5][4];


Memory management operators cont3

Memory Management Operators …cont

  • Consider the Time object example

  • new creates an object of proper size of type Time

  • Calls the default constructor

  • Returns a pointer to Time object

  • Initialize a newly created object

    Time *timePtr = new Time (12,0,0);


Memory management operators cont4

Memory Management Operators …cont

  • delete operator

  • General Form:

    deletepointer-variable

    For arrays

    delete [ ] pointer-variable


Memory management operators cont5

Memory Management Operators …cont

  • delete destroys dynamically allocated object and frees memory space

  • Example

    delete floatPtr;

    delete [ ] arrayPtr;

    delete timePtr;

  • Don’t forget to use delete [ ] for arrays


Memory management operators cont6

Memory Management Operators …cont

new *intPtr = new int (12);

..

..

delete intPtr;

While deallocating memory, following happens implicitly:

  • Value of intPtr is checked against NULL that is zero

  • If intPtr is not NULL, the memory segment to which intPtr points is deallocated


Memory management operators cont7

Memory Management Operators …cont

  • Memory leak

    for (int i=0; i < 10; i++) {

    int *intPtr = new int( 256 );

    //... Some code

    }

  • intPtr is internal to for loop, it doesn’t exist outside the loop

  • Thus link to memory allocated is permanently lost – Memory Leak

  • Underlines the importance of matching new and delete


Memory management operators cont8

Memory Management Operators …cont

  • Segmentation Fault

    int *intPtr;

    for (int i=0; i < 10; i++) {

    intPtr = new int( 256 );

    //... Some code

    delete intPtr;

    }

    //...

    delete intPtr; // segmentation fault

    Beware of such faults … core dump files


Memory management operators cont9

Memory Management Operators …cont

  • Good programming practice

    • Initialize pointer to NULL after declaration and after delete

      int *intPtr = NULL;

      for (int i=0; i < 10; i++) {

      intPtr = new int( 256 );

      //... Some code

      delete intPtr;

      intPtr = NULL;

      }

      //...

      delete intPtr;

      intPtr = NULL;


Dynamic memory

Dynamic Memory

  • Variable representation

    int num;

    num :

    int n[5];

    n :


Dynamic memory1

Dynamic Memory

struct sampleStruct {

int p;

char c;

};

sampleStruct


Dynamic memory cont

Dynamic Memory … cont

  • Pointer

    int *intPtr = new int (12);

intPtr :

12


Dynamic memory cont1

Dynamic Memory … cont

  • Consider the code

    int *foo = new int* [4];

    *foo[0] = 17;

    foo[1] = NULL;

    foo[2] = foo[1];

    *foo[3] = 42;

    What would be the memory picture generated ?


Dynamic memory cont2

Dynamic Memory … cont

foo:

17

42


Dynamic memory cont3

Dynamic Memory … cont

  • Memory Picture

    Which code generates this picture ?

1

NBA

2003

12


  • Login