The malloc () function is used to allocate memory and has the following prototype:
void * malloc (unsigned int num);
malloc(…) takes in only a single argument which is the memory required in bytes. The function gets the number of bytes that we allocate (a) allocates memory and returns a pointer void * allocated to the first byte. The void * pointer can be assigned to any type of pointer. If there is not enough memory to allocate the requested memory malloc () returns a null pointer. malloc(…) allocated bytes of memory and not blocks of memory like calloc(…). See an example of dynamic allocation with malloc ():
#include <stdio.h>
#include <stdlib.h> /* To use malloc () */
main (void)
{
int *p;
int a;
int i;
... /* Determines the value to somewhere */
p = (int *) malloc (a * sizeof (int)); /* Allocate the integers p can now be treated as a vector the positions */
if (!p)
{
printf ("** Error: Insufficient Memory **");
exit;
}
for (i = 0; i<a; i ++) /* p can be treated as a vector with the positions */
p[i] = i * i;
...
return 0;
}
In the example above, is to allocate enough memory to store the integers. The sizeof () operator returns the number of bytes of an integer. It is useful to know the size of type. The pointer void * malloc () returns is converted to an int * and the cast is assigned to p. The following statement tests whether the operation was successful. If not, p has a null value, which will cause !P returns true. If the operation is successful, we can use the whole allocated vector normally, for example by indexing the p [0] a p [(a-1)].
The calloc () function also serves to allocate memory, but has a prototype a little different:
void * calloc (unsigned int a, unsigned int size);
The function allocates an amount of memory equal to a * size, that is, allocates enough memory to a vector of a size objects. Returns a void * pointer to the first byte allocated. The void * pointer can be assigned to any type of pointer. If there is not enough memory to allocate memory required to calloc () function returns a null pointer. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size). See an example of dynamic allocation with calloc ():
#include <stdio.h>
#include <stdlib.h> /* To use calloc () */
main (void)
{
int *p;
int a;
int i;
... /* Determines the value to somewhere */
p = (int *) calloc (a, sizeof (int)); /* Allocate the integers p can now be treated as a vector the positions */
if (!p)
{
printf ("** Error: Insufficient Memory **");
exit;
}
for (i = 0; i <a; i ++) / * p can be treated as a vector with the positions * /
p [i] = i * i;
...
return 0;
}
In the above example, memory is allocated enough to put the integers. The sizeof () operator returns the number of bytes of an integer. It is useful to know the size of type. The void * pointer that calloc () returns is converted to an int * and the cast is assigned to p. The following statement tests whether the operation was successful. If not, p has a null value, which will cause! P returns true. If the operation is successful, we can use the whole allocated vector normally, for example by indexing the p [0] ap [(a-1)].