Showing posts with label Memory Management Functions. Show all posts
Showing posts with label Memory Management Functions. Show all posts

Sunday, 15 January 2012

Realloc function

Synopsis:
#include <stdio.h>
void *realloc(void *mem_address, int newsize); 
Description:
The size of the memory block pointed to by the mem_address parameter is changed to the newsize bytes, expanding or reducing the amount of memory available in the block.
In case that mem_address is NULL, the function behaves exactly as malloc, assigning a new block of newsize bytes and returning a pointer to the beginning of it.
In case that the newsize is 0, the memory previously allocated in mem_address is deallocated as if a call to free was made, and a NULL pointer is returned.
Return Value
A pointer to the reallocated memory block, which may be either the same as the mem_address argument or a new location.
Example
#include <stdio.h>
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<count;n++) printf ("%d ",numbers[n]);
  free (numbers);
  return 0;
}
It will proiduce following result:
Enter an integer value (0 to end): 2
Enter an integer value (0 to end): 3
Enter an integer value (0 to end): 4
Enter an integer value (0 to end): 5
Enter an integer value (0 to end): 0
Numbers entered: 2 3 4 5 0

malloc function

Synopsis:
#include <stdio.h>
void * malloc ( int size );
Description:
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.
Return Value
On success, a pointer to the memory block allocated by the function. If fails a NULL value is returned.
Example
#include <stdio.h>
int main ()
{
  int i,n;
  int * pData;
  printf ("Amount of numbers to be entered: ");
  scanf ("%d",&i);
  pData = (int*) malloc (i*sizeof(int));
  if (pData==NULL) exit (1);
  for (n=0;n<i;n++)
  {
    printf ("Enter number #%d: ",n);
    scanf ("%d",&pData[n]);
  }
  printf ("You have entered: ");
  for (n=0;n<i;n++) printf ("%d ",pData[n]);
  free (pData);
  return 0;
}
It will proiduce following result:
Amount of numbers to be entered: 10
Enter number #0: 2
Enter number #1: 3
Enter number #2: 3
Enter number #3: 4
Enter number #4: 5
Enter number #5: 6
Enter number #6: 7
Enter number #7: 8
Enter number #8: 3
Enter number #9: 9
You have entered: 2 3 3 4 5 6 7 8 3 9

free function

Synopsis:
#include <stdio.h>
void free(void *mem_address);
Description:
A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.
Return Value
None.
Example
#include <stdio.h>
int main ()
{
  int * buffer1, * buffer2, * buffer3;
  buffer1 = (int*) malloc (100*sizeof(int));
  buffer2 = (int*) calloc (100,sizeof(int));
  buffer3 = (int*) realloc (buffer2,500*sizeof(int));
  free (buffer1);
  free (buffer3);
  return 0;
}
It will proiduce following result:
This program has no output.
This is just demonstration of allocation and deallocation of memory.

Calloc Function

Synopsis:
#include <stdio.h>
void *calloc(int num elems, int elem_size); 
Description:
Allocates a block of memory for an array of number elements, each of them size bytes long, and initializes all its bits to zero.
Return Value
A pointer to the memory block allocated by the function.
Example
#include <stdio.h>
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<i;n++)
  {
    printf ("Enter number #%d: ",n);
    scanf ("%d",&pData[n]);
  }
  printf ("You have entered: ");
  for (n=0;n<i;n++) printf ("%d ",pData[n]);
  free (pData);
  return 0;
}
It will proiduce following result:
Amount of numbers to be entered: 10
Enter number #0: 2
Enter number #1: 3
Enter number #2: 3
Enter number #3: 4
Enter number #4: 5
Enter number #5: 6
Enter number #6: 7
Enter number #7: 8
Enter number #8: 3
Enter number #9: 9
You have entered: 2 3 3 4 5 6 7 8 3 9
host gator coupon