dynamic memory allocation(ppc)

Upload: janardhan

Post on 31-May-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    1/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur1

    Dynamic Allocation

    P. P. Chakrabarti

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    2/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur2

    Fun with scope rules:

    int data=0, test=1;

    main()

    { int i=2, j=3, test=2;

    printf("M1: i = %d, j = %d\n", i,j);

    printf("M2: test = %d, data = %d\n", test, data);

    {

    int data = 1, i =5;

    printf("B1: i = %d, j = %d\n", i,j);

    printf("B2: test = %d, data = %d\n", test, data);

    }

    printf("M3: i = %d, j = %d\n", i,j);

    printf("M4: test = %d, data = %d\n", test, data);

    }

    [ppchak]$ ./a.out

    M1: i = 2, j = 3

    M2: test = 2, data = 0

    B1: i = 5, j = 3

    B2: test = 2, data = 1M3: i = 2, j = 3

    M4: test = 2, data = 0

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    3/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur3

    Spot the difference:

    int data=0, test=1;

    main()

    { int i=2, j=3, test=2;

    printf("M1: i = %d, j = %d\n", i,j);

    printf("M2: test = %d, data = %d\n", test, data);

    {

    int data = 1; i =5;

    printf("B1: i = %d, j = %d\n", i,j);

    printf("B2: test = %d, data = %d\n", test, data);

    }

    printf("M3: i = %d, j = %d\n", i,j);

    printf("M4: test = %d, data = %d\n", test, data);

    }

    [ppchak]$ ./a.out

    M1: i = 2, j = 3

    M2: test = 2, data = 0

    B1: i = 5, j = 3

    B2: test = 2, data = 1M3: i = 5, j = 3

    M4: test = 2, data = 0

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    4/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur4

    More on scope:

    int data=0, test=1;

    main()

    { int i=2, j=3, test=2;

    printf("M1: i = %d, j = %d\n", i,j);

    printf("M2: test = %d, data = %d\n", test, data);

    fun();

    }

    fun()

    { int data =5;

    int i=6, j =7;

    printf("F1: i = %d, j = %d\n", i,j);

    printf("F2: test = %d, data = %d\n", test, data);

    }

    [ppchak]$ ./a.out

    M1: i = 2, j = 3

    M2: test = 2, data = 0

    F1: i = 6, j = 7F2: test = 1, data = 5

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    5/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur5

    Problems with static allocation

    Scenario 1: Read an integer n, read n integers and sortthem.

    [Q: How do we declare the array size?]

    Scenario 2: Make the set ADT with the insert, delete,

    member, union functions with no idea of how manyelements may exist at a given point in time.

    [Q: Can we use an array at all for this?]

    [ How to prevent space wastage in these scenarios?]

    To solve these, C provides scope for dynamically

    allocating and releasing memory using library

    functions

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    6/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur6

    Memory Allocation Process in C

    Local variables

    Free memory

    Global variables

    InstructionsPermanentstorage area

    Stack

    Heap

    Used for dynamic

    allocation

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    7/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur7

    Memory Allocation Functions

    ?malloc: Allocates requested number of bytes andreturns a pointer to the first byte of the allocated

    space.?calloc: Allocates space for an array of elements,

    initializes them to zero and then returns a pointer tothe memory.

    ? free : Frees previously allocated space.

    ? realloc: Modifies the size of previously allocatedspace.

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    8/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur8

    malloc( )main()

    {int *A, n, i;

    scanf("%d", &n);

    A = (int *) malloc(n*sizeof(int));

    for (i=0; i

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    9/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur9

    malloc( )-ing recordsmain()

    { typedef struct{

    char name[20];

    int roll;

    float SGPA[8], CGPA;

    } person;

    person *student;int i,j,n;

    scanf("%d", &n);

    student = (person *)malloc(n*sizeof(person));

    for (i=0; i

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    10/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur10

    Arrays of pointers

    #define N 20

    #define M 10main()

    { char word[N], *w[M];

    int i, n;

    scanf("%d",&n);for (i=0; i

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    11/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur11

    w

    0

    1

    2

    3

    9

    How it will look like

    \0rakludneT

    \0varuoS

    \0nahK

    \0aidnI

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    12/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur12

    Dynamic Arrays of pointers

    #define N 20

    main(){

    char word[N], **w; /* **w is a pointer to a pointer array */

    int i, n;

    scanf("%d",&n);

    w = (char **) malloc (n * sizeof(char *));for (i=0; i

  • 8/15/2019 Dynamic Memory Allocation(PPC)

    13/13

    13-03-03 P.P.Chakrabarti, IIT Kharagpur13

    w

    0

    1

    2

    3

    4

    How this will look like

    \0aidnI

    \0aknaLirS

    \0ailartsuA

    \0ayneK

    N \0dnalaeZwe