16531_malloccalloc

Embed Size (px)

Citation preview

  • 8/12/2019 16531_malloccalloc

    1/5

    malloc()

    malloc()function allocate a block of byte of memory in byte. In this when the memory block

    needed explicitly requested. The malloc() function is same as a function is request for RAMin the system memory. If the request is rant then a !oid pointer return and

    the pointer point start of that block. If the request fail then a "#$$ pointer return.

    %xample&

    malloc( number of element * size of each element);

    int * ptr;ptr = malloc(10*sizeof(int));

    Where size represents the memory required in bytes .The memory hich is pro!ided isconti"uous memory.

    #ut malloc function return !oid pointer so it needed type castin" of that pointer.

    $%amlpe&(type cast)malloc( number of element * size of each element);

    int * ptr;

    ptr =(int*) malloc(10*sizeof(int));

    similarly for allocation of memory to a structure !ariable &

    $%amlpe&

    (struct name)malloc( sizeof(struct name));

    struct employee

    '

    int empid;char empname0+;

    float empcontactno;

    ,;

    struct employee *ptr

    ptr=(struct employee*)malloc(sizeof(struct employee));

    calloc()

  • 8/12/2019 16531_malloccalloc

    2/5

    -n malloc requested memory is pro!ided a bloc of conti"uous memory . calloc() function is

    similar to the malloc rather then calloc() function

    allocated the memory bloc for an array of elements. /emory for a "roup of obects usedcalloc() function. -f calloc() function is e%ecuted

    succesfully then its allocated memory is set as zero and a pointer returned and if the function

    failed then a 233 pointer return.

    $%ample&

    void *calloc(size_t

    number,size_t size);

    sizet used for unsi"ned on most compilers.The number is the number of obects hich is

    allocate4 and size is the size (in bytes) of each obect.

    int main () '

    int number4i;

    printf(5$nter the number 5);scanf(56d547number);

    printf(5umber hich is here are54number);

    int *ptr = (int *) calloc (number4sizeof(int));for (i=0; i8number;i99) '

    ptri+ = i 9i;

    ,for (i=0; i8number;i99) '

    printf(5:esult is 6d 6dn54i4ptri+);

    ,,

    free()

  • 8/12/2019 16531_malloccalloc

    3/5

    $%ample&

    free(ptr);

    int main () '

    int number4i;

    printf(5$nter the number 5);

    scanf(56d547number);

    printf(5umber hich is here are54number);

    for (i=0; i8number;i99) '

    ptri+ = i 9i;,

    for (i=0; i8number;i99) '

    printf(5:esult is 6d 6dn54i4ptri+);

    ,

    free(ptr)'

    realloc()

    realloc() function is used for resize the size of memory bloc hich is allocated by the malloc()and calloc () function.

    To situation here use realloc() function.

    hen allocated block is insufficient need more memory then use realloc().

    hen allocated memory is much more then the required application then use

    realloc().

    $%ample&

  • 8/12/2019 16531_malloccalloc

    4/5

    realloc(ptr, new size);

    * Throu"h realloc() resize the memory . *

    >include 8stdio.h?

    >include 8stdlib.h?>include 8strin".h?

    '

    charbuffer@0+4 *ms";

    * -nput a strin". *

    puts(5$nter the te%t line5);

    "ets(buffer);

    * The strin" copied in to initial allocated bloc *

    ms" = realloc(2334 strlen(buffur)91);

    strcpy(ms"4 buffer);

    * Aisplay the messa"e hich copied. *

    puts(messa"e);

    * Bet another strin" from the user. *

    puts(5$nter another te%t line.5); ;

  • 8/12/2019 16531_malloccalloc

    5/5

    "ets(buffer);

    * :esize the memory and also concatenate the strin" to it. *

    ms" = realloc(ms"4(strlen(ms") 9 strlen(buffer)91));

    strcat(ms"4 buffer);

    ,