cmsc 202
Download
Skip this Video
Download Presentation
CMSC 202

Loading in 2 Seconds...

play fullscreen
1 / 19

CMSC 202 - PowerPoint PPT Presentation


  • 101 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CMSC 202' - theta


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 cont3
Dynamic Memory … cont
  • Memory Picture

Which code generates this picture ?

1

NBA

2003

12

ad