14654_Dynamic Memory Alloc

Embed Size (px)

Citation preview

  • 8/3/2019 14654_Dynamic Memory Alloc

    1/3

    callocfunction

    void * calloc ( size_t num, size_t size );

    Allocate space for array in memory

    Allocates a block of memory for an array ofnum elements, each of them size bytes long, and initializes all its bits to zero.

    The effective result is the allocation of an zero-initialized memory block of ( num * size) bytes.

    Parameters

    numNumber of elements to be allocated.

    size

    Size of elements.

    Return Value

    A pointer to the memory block allocated by the function.

    The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable.

    If the function failed to allocate the requested block of memory, a NULL pointer is returned.

    Example

    123456789

    10111213141516171819202122

    /* calloc example */#include #include

    int main (){ int i,n; int * pData;printf ("Amount of numbers to be entered: ");

    scanf ("%d",&i);pData = (int*) calloc (i,sizeof(int));

    if (pData==NULL) exit (1); for (n=0;n

  • 8/3/2019 14654_Dynamic Memory Alloc

    2/3

    void * realloc ( void * ptr, size_t size );

    Reallocate memory block

    The size of the memory block pointed to by the ptrparameter is changed to the size bytes, expanding or reducing the amount of memory available in theblock.

    The function may move the memory block to a new location, in which case the new location is returned. The content of the memory block is preserved up

    to the lesser of the new and old sizes, even if the block is moved. If the new size is larger, the value of the newly allocated portion is indeterminate.

    In case that ptr is NULL, the function behaves exactly asmalloc, assigning a new block ofsize bytes and returning a pointer to the beginning of it.

    In case that the size is 0, the memory previously allocated in ptr is deallocated as if a call to freewas made, and a NULL pointer is returned.

    Parameters

    ptr

    Pointer to a memory block previously allocated withmalloc,callocorrealloc to be reallocated.

    If this is NULL, a new block is allocated and a pointer to it is returned by the function.

    size

    New size for the memory block, in bytes.If it is 0 and ptrpoints to an existing block of memory, the memory block pointed by ptr is deallocated and a

    NULL pointer is returned.

    Return Value

    A pointer to the reallocated memory block, which may be either the same as the ptr argument or a new location.

    The type of this pointer is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.If the function failed to allocate the requested block of memory, a NULL pointer is returned.

    Example

    12345

    6789

    101112131415161718

    1920212223242526

    /* realloc example: rememb-o-matic */#include #include

    int main ()

    { int input,n; int count=0; int * numbers = NULL;

    do {printf ("Enter an integer value (0 to end): ");scanf ("%d", &input);count++;numbers = (int*) realloc (numbers, count * sizeof(int));

    if (numbers==NULL){ puts ("Error (re)allocating memory"); exit (1); }

    numbers[count-1]=input;

    } while (input!=0);

    printf ("Numbers entered: "); for (n=0;n

  • 8/3/2019 14654_Dynamic Memory Alloc

    3/3

    The program prompts the user for numbers until a zero character is entered. Each time a new value is introduced the memory block pointed by numbers

    is increased by the size of an int.