Malloc in C – Allocation a Block of Memory

A block of memory may be allocated using the function malloc in c. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form:
ptr = (cast-type *) malloc(byte-size);
ptr is a pointer of type cast-type. The malloc in c returns a pointer (of cast-type) to an area of memory with size byte size.

Example : X = (int *) malloc (100 *sizeof(int)); On successful execution of this statement, a memory space equivalent to “100 times the size of an int’ bytes is reserved and the address of the first byte of the memory allocated is assigned to the pointer x of type of int.
Similarly, the statement cptr = (char*) malloc(10);

allocates 10 bytes of space for the pointer cptr of type char. This is illustrated as:

Note that the storage space allocated dynamically has no name and therefore its contents can be accessed only through a pointer.

We may also use malloc to allocate space for complex data types such as structures. Example:
st_var = (struct store *)malloc (sizeof (struct store));
where, st_var is a pointer of type struct store
Remember, the malloc allocates a block of contiguous bytes. The allocation can fail if the space in the heap is not sufficient to satisfy the request. If it fails, it returns a NULL. We should therefore check whether the allocation is successful before using the memory pointer. This is illustrated in the program.

The program is given in Fig. It tests for availability of memory space of required size. If it is available, then the required space is allocated and the address of the first byte of the space allocated is displayed. The program also illustrates the use of pointer variable for storing and accessing the table values.

          #include <stdio.h>
          #include <stdlib.h>
          #define NULL 0

               int *p, *table;
               int size;
               printf("\nWhat is the size of table?");
                 /*----------Memory allocation----------*/
               if((table = (int*)malloc(size *sizeof(int))) ==NUL)
                    printf("No space available \n");
          printf("\n Address of the first byte is %u\n", table);
          /* Reading table values*/
          printf("\nInput table values\n");

          for (p=table; p<table + size; p++)
          /* Printing table values in reverse order*/
          for (p = table + size -1; p >= table; p--)
                printf("%d is stored at address %u \n", *p,p);

What is the size of the table? 5
Address of the first byte is 2262
Input table values
11 12 13 14 15
15 is stores at address 2270
14 is stores at address 2268
13 is stores at address 2266
12 is stores at address 2264
11 is stores at address 2262
Read More Topics
Advantages of Using the DBMS Approach
Workers Behind the Scene in DBMS
Characteristics of the Database Approach
An Example of Databases and Database Users
Nesting of Function in C
Passing Arrays to Function in C
Arrays Programming in ANSI C

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply