dynamic objects

Upload: suganya-selvaraj

Post on 03-Jun-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Dynamic Objects

    1/24

    DynamicObjects

  • 8/12/2019 Dynamic Objects

    2/24

    Memory Management

    Static Memory Allocation

    Memory is allocated at compilation time

    Dynamic Memory

    Memory is allocated at running time

  • 8/12/2019 Dynamic Objects

    3/24

    Static vs. Dynamic Objects

    Static object

    (variables as declared in function calls)

    Memory is acquired

    automatically

    Memory is returned

    automatically when object

    goes out of scope

    Dynamic object

    Memory is acquired by

    program with an allocation

    request newoperation

    Dynamic objects can exist

    beyond the function in which

    they were allocated

    Object memory is returned by

    a deallocation request

    deleteoperation

  • 8/12/2019 Dynamic Objects

    4/24

    Memory Allocation

    {int a[200];

    }

    int* ptr;

    ptr = new int[200];

    delete [] ptr;

    new

    delete

  • 8/12/2019 Dynamic Objects

    5/24

    Object (variable) creation:New

    Syntaxptr = new SomeType;

    where ptris a pointer of type SomeType

    p

    Uninitialized int variable

    Example

    int* p = new int;

  • 8/12/2019 Dynamic Objects

    6/24

    Object (variable) destruction: Delete

    Syntax

    delete p;

    storage pointed to by p is returned to free store and p is now undefined

    p

    Exampleint* p = new int;

    *p = 10;

    delete p;

    10

  • 8/12/2019 Dynamic Objects

    7/24

    Array of New:

    dynamic arrays

    Syntax

    P = new SomeType[Expression];

    Where Pis a pointer of type SomeType

    Expressionis the number of objects to be

    constructed -- we are making an array

    Because of the flexible pointer syntax, Pcan be

    considered to be an array

  • 8/12/2019 Dynamic Objects

    8/24

    Example

    Dynamic Memory Allocationn Request for unnamedmemory from the Operating System

    n int *p, n=10;

    p = new int;

    p = new int[100];

    p

    new

    p

    new

    p = new int[n]; p

    new

  • 8/12/2019 Dynamic Objects

    9/24

    Memory Allocation Example

    Want an array of unknown size#include

    using namespace std;

    void main()

    {

    int n;

    cout > n;

    int *grades = new int[n];

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

    int mark;

    cout

  • 8/12/2019 Dynamic Objects

    10/24

    Freeing (or deleting) Memory

  • 8/12/2019 Dynamic Objects

    11/24

    A Simple Dynamic List Example

    cout > n;

    int *A = new int[n];

    if(n

  • 8/12/2019 Dynamic Objects

    12/24

    Initialize

    void initialize(int list[], int size, int value){

    for(int i=0; i

  • 8/12/2019 Dynamic Objects

    13/24

    print()

    void print(int list[], int size) {

    cout

  • 8/12/2019 Dynamic Objects

    14/24

    Adding Elements

    // for adding a new element to end of array

    int* addElement(int list[], int& size, int value){

    int* newList = new int [size+1]; // make new array

    if(newList==0){

    cout

  • 8/12/2019 Dynamic Objects

    15/24

  • 8/12/2019 Dynamic Objects

    16/24

    Adding Element (version 2)

    // for adding a new element to end of array

    // here list is a reference to a pointer variable: if the value ofthe pointer is changed in function, the change is global.

    void addElement( int * & list, int & size, const int value ){

    int * newList = new int [size + 1];

    if( newList == NULL ){

    cout

  • 8/12/2019 Dynamic Objects

    17/24

    Deleting Element (version 2)

    void deleteFirst( int * & list, int & size ){

    if( size

  • 8/12/2019 Dynamic Objects

    18/24

    Another Main program

    int main(){

    int * A = NULL;

    int size = 0;

    int i;

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

    addElement( A, size, i );

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

    cout

  • 8/12/2019 Dynamic Objects

    19/24

    Dangling Pointer Problem

    int *A = new int[5];

    for(int i=0; i

  • 8/12/2019 Dynamic Objects

    20/24

    Memory Leak Problem

    int *A = new int [5];

    for(int i=0; i

  • 8/12/2019 Dynamic Objects

    21/24

    A Dynamic 2D Array

    A dynamic array isan array of pointersto save space whennot all rows of thearray are full.

    int **table;

    32 18 2412

    42 141912161113

    1811

    13 1413

    22

    table = new int*[6];

    table[0] = new int[4];

    table[1] = new int[7];

    table[2] = new int[1];

    table[3] = new int[3];

    table[4] = new int[2];

    table[5] = NULL;

    table[0]

    table[1]

    table[2]

    table[3]

    table[4]

    table[5]

    table

  • 8/12/2019 Dynamic Objects

    22/24

  • 8/12/2019 Dynamic Objects

    23/24

    Memory Deallocation

    Memory leak is a serious bug!

    Each row must be deleted individually

    Be careful to delete each row before deletingthe table pointer.

    for(int i=0; i

  • 8/12/2019 Dynamic Objects

    24/24

    int m, n;

    cin >> m >> n >> endl;

    int** mat;

    mat = new int*[m];

    for (int i=0;i> m >> n >> endl;

    int** mat;

    mat = imatrix(m,n);

    int** imatrix(nr, nc) {

    int** m;m = new int*[nr];

    for (int i=0;i