C – Malloc 2017-08-07T14:18:09+00:00

Learn how to use malloc function with example in C programming Language

The malloc()  function dynamically allocates memory when required. This function allocates ‘size’ byte of memory and returns a pointer to the first byte or NULL if there is some kind of error.span>

Format is as follows.
void * malloc (size_t size);

Specifies in bytes the size of the area you want to reserve the argument. It returns the address as the return value of the dynamically allocated area. In addition, returns NULL if it fails to secure the area. The failure to ensure that the situation is usually that is out of memory.

The return type is of type void *, also receive the address of any type. The fact is used as follows.

double * p = (double *) malloc (sizeof (double));

The size of the area using the sizeof operator like this. The return value is of type void *, variable in the receiving side can be the pointer of any type in the host language C language called C + +, the pointer type to other type void * is, without casting, so can not be assigned, Use a cast. In C, a pointer type to void * type from another, so that the cast automatically, there is no need to explicitly cast originally.Of course, should not write because not explicitly given as well as portability to C + +, I wrote better.

In addition, the secured area is unknown at this point that’s on it? That is the same as a normal state to declare a local variable. Thus, with reference values ??must not be left without initialization.

With this feature, you get a pointer to an allocated block of memory. Its structure is:


pointer = (type) malloc (size in bytes);

An example:


int * p;
p = (int *) malloc (sizeof (int));
* p = 5;

First we declare a pointer, which is still pointing nowhere. Then the pointer, not the content but the pointer itself is equal to a pointer type int that contains the memory address space for an int. Sizeof () gets the space it occupies what you want, if you put int in, such as 2 bytes, because we have assigned two bytes. This feature also serves to get the size of pointers, variables, or whatever it takes.
Finally, now that the pointer is contained, we give a value.

For example:
int *ptr = malloc(sizeof(int) * 10);      // allocates 10 ints!

If it is unable to find the requested amount of memory, malloc() function returns NULL. So you should really check the result for errors:

int *ptr = malloc(sizeof(int) * 5000);
if (ptr == NULL)
printf(” Out of memory!\n”);

There are only two ways to get allocated memory back. They are exit from the program and calling free() to free function. If your program runs a while and keeps malloc()ing and never free()ing when it should, it is said to “leak” memory. Make sure to avoid memory leaks! free() that memory when you are done with it!


Output :

Default values are :

i = 1526          *i=3777

i = 1528          *i=29472

i = 1530          *i=-6138

i = 1532          *i=3900

i = 1534          *i=2280

New values :

i = 1526          *i=1

i = 1528          *i=2

i = 1530          *i=3

i = 1532          *i=4

i = 1534          *i=5

Prev Next