Thursday 22 April 2021

Memory Allocation Function

 

1. Malloc

Full form of malloc function is memory allocate. malloc allocates a block of size bytes from the memory heap. It allows a program to allocate memory explicitly as it's needed, and in the exact amounts needed.

The heap is used for dynamic allocation of variable-sized blocks of memory. Many data structures, such as trees and lists, naturally employ heap memory allocation. All the space between the end of the data segment and the top of the program stack is available for use in the small data models, except for a small margin immediately before the top of the stack. This margin is intended to allow the application some room to make the stack larger, in addition to a small amount needed by DOS. In the large data models, all the space beyond the program stack to the end of available memory is available for the heap.

Syntax 


Declaration:  void *malloc(size_t size);

Return Value


  • On success, malloc returns a pointer to    the newly allocated block of memory.
  • On error (if not enough space exists for  the new block), malloc returns null. The   contents of the block are left unchanged.
  • if the argument size == 0, malloc returns   null.

Example : Single link list node creation function using malloc function which allocating dynamic memory for a node.

typedef struct n

{

      int data;

      struct n *next;

}node;

 

node* create(int n)

{

      node *nw;

      nw = (node*)malloc(sizeof(node));

      nw->data = n;

      nw->next = NULL;

      return nw;

}

void main()

{

      node  *nw = create(n);

}


2. calloc



calloc provides access to the C memory heap, which is available for dynamic allocation of variable-sized blocks of memory.

Many data structures, such as trees and lists, naturally employ heap memory allocation.

calloc allocates a block (nitems * size) bytes and clears it to 0. To allocate a block larger than 64K, use farcalloc.

Syntax 

 void *calloc(size_t nitems, size_t size);

 Small Data Models
 
All the space between the end of the data segment and the top of the program stack is available for use in the tiny, small, and medium models, except for a small margin immediately before the top of the stack.

This margin allows room for the application to grow on the stack, plus a small amount needed by DOS.

 Large Data Models

In the compact, large, and huge models, all space beyond the program stack to the end of physical memory is available for the heap.

 Return Value:

  • On success, returns a pointer to the newly allocated block.
  • On failure (not enough space exists for the new block, or nitems or     size is 0), returns null.
Example 

int main(void)

{

   char *str = NULL;

 

   /* allocate memory for string */

   str = (char *) calloc(10, sizeof(char));

 

   /* copy "Hello" into string */

   strcpy(str, "Hello");

 

   /* display string */

   printf("String is %s\n", str);

 

   /* free memory */

   free(str);

 

   return 0;

}

 



Next Topic

No comments:

Post a Comment