comp322_03

Embed Size (px)

Citation preview

  • 8/4/2019 comp322_03

    1/24

    Memory Management

    Junaed SattarSeptember 17, 2008

    Lecture 3

  • 8/4/2019 comp322_03

    2/24

    Today

    memory management

    C++ memory management issues

    dynamic memory (de)allocation

    pointers

  • 8/4/2019 comp322_03

    3/24

    Memory structure (simplified)

    Command line argumentsEnvironment variables

    Stack(Function/method arguments, locals, return addresses)

    Free store (Heap)

    Dynamically allocated memory

    High address

    Low address

    Code (Read-only)Uninitialized/Initialized data (No eXecute)

  • 8/4/2019 comp322_03

    4/24

    Memory basics

    C++ (as well as C) provides mechanismsfor dynamic memory allocation

    ask for memory as we need it

    let it go when we don't

    possible to write memory-efficient code(small memory footprint)

  • 8/4/2019 comp322_03

    5/24

    Memory basics

    The catch(es):

    Somewhat complex mechanism fordoing so

    Manual (i.e. programmer-initiated)memory management

    Coding mistakes leads to ugly, hard-to-

    find, hard-to-replicate bugs No automatic garbage collection

  • 8/4/2019 comp322_03

    6/24

    One scenario

    Loading an image from disk for display

    locate the file and format (jpg/tiff/bmp/png...)

    allocate enough memory to hold the image

    copy the image from disk to memory

    process

    deallocate the memory previously allocated

    Theallocate-use-deallocatecyclemust be maintained

  • 8/4/2019 comp322_03

    7/24

    Graphically...

    find image

    Main memory

    Secondarymemory

  • 8/4/2019 comp322_03

    8/24

    Graphically...

    find imagecalculate size

    Main memory

    Secondarymemory

    645KBytes

  • 8/4/2019 comp322_03

    9/24

    Graphically...

    find imagecalculate sizeallocate memory

    dynamicallyallocate

    Main memory

    Secondarymemory

    645KBytes

  • 8/4/2019 comp322_03

    10/24

    Graphically...

    find imagecalculate sizeallocate memory

    Main memory

    Secondarymemory

    645KBytes

  • 8/4/2019 comp322_03

    11/24

    Simple workflow, but...

    Task for the programmer to do all that

    unlike Java, where the compiler/interpretertakes care of it

    No automatic garbage collection Programmers must clean up themselves

    Memory allocation requests might fail

    what if not enough free memory?

  • 8/4/2019 comp322_03

    12/24

    Pointers

    holds the address of a data object or afunction/method

    handler to dynamically allocated memory

    pass arguments as reference

    we've seen this last class

  • 8/4/2019 comp322_03

    13/24

    Pointers

    declaration

    int *i;

    char *c;

    and so on.

    refers to objects of one data type only

    i.e. the same pointer cannot refer to ints and

    chars

  • 8/4/2019 comp322_03

    14/24

    Pointers

    simple usage:

    int number = 10;

    int *i = &number;

    *i is a pointer to an int

    i contains the address ofnumber

    *i contains the value stored in number i.e. *i has the value stored at the address pointed

    to by i

  • 8/4/2019 comp322_03

    15/24

    Pointers

    stray pointer:

    int *i;

    i points to what?

    undetermined, random address

    Stray or uninitialized pointer.

    using a pointer without initialization has

    undefined results in other words, big bad bug.

  • 8/4/2019 comp322_03

    16/24

    Bad Pointer

    int *i;

    int num = 10;

    ...

    *i = 100; // Where is this going to???

    This program:

    will compile,will run, and maybe will not even crash depending on what i points to, it might bring down

    your program entirely

    always initialize with either a valid address or NULL

  • 8/4/2019 comp322_03

    17/24

    Allocation

    usage:

    int *i = new int;

    *i is a pointer to an int, but also we have:

    dynamically allocated space for one int

    using the C++ new operator

    new provides dynamic storage allocation

    find storage in free store (or heap)

    returns a pointer to the object created

  • 8/4/2019 comp322_03

    18/24

    More about new

    new can fail

    if it does, then either returns NULL or throwsan exception (std::bad_alloc)

    can be used to initialize the memory: int *i = new int(10);

    note the bracket type

    can be used to allocate arrays: int *i = new int[10];

  • 8/4/2019 comp322_03

    19/24

    new example

    char* pCharArray;

    try {

    pCharArray = new char[256];

    // sample usagestrcpy(pCharArray, "Array of characters");

    }

    catch( std::bad_alloc e )

    {

    std::cerr

  • 8/4/2019 comp322_03

    20/24

    Deallocation

    an object created with new exists untiloperator delete is used to deallocate

    will not be implicitly called for an object

    created with a new must explicitly deallocate

    otherwise will create memory holes

  • 8/4/2019 comp322_03

    21/24

    Deallocation

    new and delete must come in pairs

    use correct matches

    use delete() with new() (individual objects)

    use delete[]() with new[]() (arrays)

    if not, the result is undefined

    deleting a NULL pointer has no effect.

    delete does not assign NULL to a pointer

    accessing a deleted pointer hasundefined effects

  • 8/4/2019 comp322_03

    22/24

    delete example

    char* pCharArray = 0;

    try {

    pCharArray = new char[256];

    }catch( std::bad_alloc e )

    {

    std::cerr

  • 8/4/2019 comp322_03

    23/24

    memory holes?

    void SomeFunction() {

    char* pCharArray = 0;

    try {

    pCharArray = new char[256];

    }catch( std::bad_alloc e ){

    std::cerr

  • 8/4/2019 comp322_03

    24/24

    memoryF.A.Q.

    why new and not malloc?

    support for objects and type-safe pointers

    double delete?

    causes prorgam to crash

    mixing new/malloc?

    bad idea.