L7-2_ Dynamic Memory Allocation

download L7-2_ Dynamic Memory Allocation

of 16

Transcript of L7-2_ Dynamic Memory Allocation

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    1/16

    Dynamic Memory Allocation

    Spring Semester 2011 Programming and Data Structure 51

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    2/16

    Basic Idea

    Many a time we face situations where dataare dynamic in nature.

    Amount of data cannot be predictedbeforehand.

    Spring Semester 2011 Programming and Data Structure 52

    um er o a a ems eeps c ang ng ur ngprogram execution.

    Such situations can be handled more

    easily and effectively using dynamicmemory management techniques.

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    3/16

    Contd.

    C language requires the number of elementsin an array to be specified at compile time.

    Often leads to wastage or memory space orprogram failure.

    Spring Semester 2011 Programming and Data Structure 53

    - a ows s, owever Dynamic Memory Allocation

    Memory space required can be specified at the

    time of execution.

    C supports allocating and freeing memorydynamically using library routines.

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    4/16

    Memory Allocation Process in C

    Local variables

    Free memory

    Global variables

    Stack

    Heap

    Spring Semester 2011 Programming and Data Structure 54

    Instructions

    storage area

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    5/16

    Contd.

    The program instructions and the globalvariables are stored in a region known aspermanent storage area.

    The local variables are stored in another

    Spring Semester 2011 Programming and Data Structure 55

    area ca e s ac . The memory space between these two

    areas is available for dynamic allocation

    during execution of the program. This free region is called the heap.

    The size of the heap keeps changing.

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    6/16

    Memory Allocation Functions

    malloc

    Allocates requested number of bytes and returns

    a pointer to the first byte of the allocated space. calloc

    Allocates space for an array of elements,

    Spring Semester 2011 Programming and Data Structure 56

    initializes them to zero and then returns a pointerto the memory.

    free

    Frees previously allocated space.

    realloc

    Modifies the size of previously allocated space.

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    7/16

    Allocating a Block of Memory

    A block of memory can be allocated usingthe functionmalloc.

    Reserves a block of memory of specified sizeand returns a pointer of type void.

    Spring Semester 2011 Programming and Data Structure 57

    e re urn po n er can e ype-cas e o anypointer type.

    General format:

    ptr = (type *) malloc (byte_size);

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    8/16

    Contd.

    Examples

    p = (int *) malloc(100 * sizeof(int));

    A memory space equivalent to 100 times the sizeof an intb tes is reserved.

    Spring Semester 2011 Programming and Data Structure 58

    The address of the first byte of the allocatedmemory is assigned to the pointerp of type int.

    p

    400 bytes of space

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    9/16

    Contd.

    cptr = (char *) malloc (20);

    Allocates 20 bytes of space for the pointer cptr oftype char.

    Spring Semester 2011 Programming and Data Structure 59

    sptr = (struct stud *) malloc

    (10 * sizeof (struct stud));

    Allocates space for a structure array of 10elements. sptr points to a structure element oftype struct stud.

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    10/16

    Points to Note

    malloc always allocates a block of

    contiguous bytes.

    The allocation can fail if sufficient contiguousmemory space is not available.

    Spring Semester 2011 Programming and Data Structure 60

    ,ma oc .

    if ((p = (int *) malloc(100 * sizeof(int))) == NULL)

    {

    printf (\n Memory cannot be allocated);

    exit();

    }

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    11/16

    Releasing the Used Space

    When we no longer need the data stored in ablock of memory, we may release the block for

    future use.

    How?

    Spring Semester 2011 Programming and Data Structure 61

    General syntax:

    free (ptr);

    whereptr is a pointer to a memory block whichhas been previously created usingmalloc.

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    12/16

    Example 1: using 1-D array

    printf("Input heights for %d

    students \n",N);

    for (i=0; i

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    13/16

    Example 2: Bubble sort on array of structures

    #include

    typedef struct

    {

    int roll;

    char dept_code[25];float cgpa;

    } stud;

    for (k=0; k

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    14/16

    Some Points

    class is a pointer to the starting address of the

    allocated memory block.

    We can therefore access the individual elementsof the structure array using array notation:

    , . , .

    As an alternative, we can also access usingpointersand the arrow notation:

    Example: (class+k), (class+k)->roll, etc.

    Spring Semester 2007 Programming and Data Structure 64

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    15/16

    Altering the Size of a Block

    Sometimes we need to alter the size of somepreviously allocated memory block.

    More memory needed. Memory allocated is larger than necessary.

    Spring Semester 2011 Programming and Data Structure 65

    By using the realloc function.

    If the original allocation is done as:

    ptr = malloc (size);

    then reallocation of space may be done as:

    ptr = realloc (ptr, newsize);

  • 7/29/2019 L7-2_ Dynamic Memory Allocation

    16/16

    Contd.

    The new memory block may or may not begin atthe same place as the old one.

    If it does not find space, it will create it in anentirely different region and move the contentsof the old block into the new block.

    Spring Semester 2011 Programming and Data Structure 66

    The function guarantees that the old dataremains intact.

    If it is unable to allocate, it returnsNULL and frees

    the original block.